473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reflection: Get Methods with a Specific Custom Attribute

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());
}
}
Oct 25 '07 #1
3 8869
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

Oct 25 '07 #2
Hi,

Take a look at MemberInfo.GetCustomAttributes method.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:28**********************************@microsof t.com...
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());
}
}

Oct 25 '07 #3
randy1200 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.
a) As has been said, you need to use MemberInfo.GetCustomAttributes(...)
b) You probably also want to look into MemberInfo.IsDefined(...)

Alun Harford
Oct 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ParkerV | last post by:
I am building a generic data access component. The module pulls a list of all the OLE DB providers currently installed on the machine (using the registry) and allows the user to select a...
2
by: Andrea Gavana | last post by:
Hello NG, this may seem a stupid (or even impossible) question, but my knowlegde of Python is quite limited. I have basically a simple graphical user interface that contains a Panel, another...
11
by: Ian | last post by:
Hi, I am writing an application and am creating a plug-in interface for it. The application will need to 'find' the plug-in's and will then need to find all accessible methods. The...
5
by: Wiktor Zychla | last post by:
Hello, I still have some problems with custom attributes and I ask someone for a helpful hand. In the appendix is the trivial ILAsm code with single custom attribute. The code compiles OK but...
2
by: Philip Limbeck | last post by:
Hi, I am currently stuck with a problem concerning reflection. We have to remove a Custom class attribute from an instance before giving the instance to another subsystem. Is this possible...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
7
by: Paul Hadfield | last post by:
Hi, I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system...
26
by: tjhnson | last post by:
Hi, With properties, attributes and methods seem very similar. I was wondering what techniques people use to give clues to end users as to which 'things' are methods and which are attributes. ...
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.