Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / July 2005

Tip: Looking for answers? Try searching our database.

Reflection Help with Custom Attributes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
William Wallace - 28 Jul 2005 23:41 GMT
I am unable to access the DescriptionAttribute of a method when I load the
assembly using the Assembly.ReflectionOnlyLoadFrom. After I load the assembly
I call GetTypes and loop through the types and call GetMethods. I then Loop
through the methods using CustomAttributeData.GetCustomAttributes to get the
attributes, however I can not figure out how to retrieve the
DescriptionAttribute. I had to switch back to using the regular
Assembly.LoadFrom to get it to work. I can not find any examples on how to
work with the CustomAttributeData object nor any documentation on the object
or process of using reflection to retrieve information on assemblies you have
no intention of instantiating.  Can anyone help me with the process of using
reflection in the ReflectionOnly universe for retrieving Attribute info
(DescriptionAttribute specificall) ?

Signature

William Wallace

Mattias Sjögren - 29 Jul 2005 08:26 GMT
>I can not find any examples on how to
>work with the CustomAttributeData object nor any documentation on the object
>or process of using reflection to retrieve information on assemblies you have
>no intention of instantiating.  Can anyone help me with the process of using
>reflection in the ReflectionOnly universe for retrieving Attribute info
>(DescriptionAttribute specificall) ?

Something like this

// Preload the System assembly dependency
Assembly.ReflectionOnlyLoad(typeof(DescriptionAttribute).Assembly.FullName);

// Load your assembly containing the method with the description
Assembly a = Assembly.ReflectionOnlyLoadFrom("your.dll");
MethodInfo mi = a.GetType("´TheClass").GetMethod("TheMethod");

foreach (CustomAttributeData cad in
CustomAttributeData.GetCustomAttributes(mi))
{
 // Verify the attribute type is correct and that the
 // parameterless constructor wasn't used.
 if (cad.Constructor.DeclaringType.FullName ==
"System.ComponentModel.DescriptionAttribute" &&
cad.Constructor.GetParameters().Length > 0 &&
cad.ConstructorArguments[0].ArgumentType == typeof(string))
 {
   Console.WriteLine((string)cad.ConstructorArguments[0].Value);
 }
}

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

William Wallace - 29 Jul 2005 09:16 GMT
thx, thats just where i got stuck :)
Signature

William Wallace
DarkNoir@community.microsoft.com

> >I can not find any examples on how to
> >work with the CustomAttributeData object nor any documentation on the object
[quoted text clipped - 27 lines]
>
> Mattias

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.