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 / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Reflection: Get Methods with a Specific Custom Attribute

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
randy1200 - 25 Oct 2007 14:26 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.

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

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.