The code below goes through all the methods in the given dll. I'm wondering
how I can modify this to only get methods with a specific custom attribute.
I'm not seeing any Custom Attributes like [TestMethod()] when I print out the
attributes.
Thanks,
Randy
Assembly a = Assembly.LoadFile(s);
Type[] types = a.GetTypes();
foreach (Type t in types)
{
MethodInfo[] methods = t.GetMethods();
foreach (MethodInfo m in methods)
{
MethodAttributes ma = m.Attributes;
Console.WriteLine(ma.ToString());
}
}
Jon Skeet [C# MVP] - 25 Oct 2007 14:30 GMT
On Oct 25, 2:26 pm, randy1200 <randy1...@discussions.microsoft.com>
wrote:
> The code below goes through all the methods in the given dll. I'm wondering
> how I can modify this to only get methods with a specific custom attribute.
> I'm not seeing any Custom Attributes like [TestMethod()] when I print out the
> attributes.
You need to call GetCustomAttributes(), not use the Attributes
property. The latter is just for things like "abstract" and "virtual".
Note also that if you want to get non-public methods, you'll need to
specify a BindingFlags in the call to GetMethods().
Jon
Ignacio Machin ( .NET/ C# MVP ) - 25 Oct 2007 15:32 GMT
Hi,
Take a look at MemberInfo.GetCustomAttributes method.

Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
> The code below goes through all the methods in the given dll. I'm
> wondering
[quoted text clipped - 19 lines]
> }
> }
Alun Harford - 25 Oct 2007 18:32 GMT
> The code below goes through all the methods in the given dll. I'm wondering
> how I can modify this to only get methods with a specific custom attribute.
> I'm not seeing any Custom Attributes like [TestMethod()] when I print out the
> attributes.
a) As has been said, you need to use MemberInfo.GetCustomAttributes(...)
b) You probably also want to look into MemberInfo.IsDefined(...)
Alun Harford