Groups | Blog | Home
all groups > dotnet sdk > july 2005 >

dotnet sdk : Reflection Help with Custom Attributes


William Wallace
7/28/2005 3:41:04 PM
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) ?



--
William Wallace
William Wallace
7/29/2005 1:16:02 AM
thx, thats just where i got stuck :)
--
William Wallace
DarkNoir@community.microsoft.com


[quoted text, click to view]
Mattias Sjögren
7/29/2005 9:26:26 AM

[quoted text, click to view]

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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button