By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

C# reflection help please...

P: 22
Hi guys,
I am using reflection to get methods in a .dll file but i want to get only user defined methods in the file. Here is what i do;

Expand|Select|Wrap|Line Numbers
  1. Assembly assembly = Assembly.LoadFile(assemblyName);
  2. Type[] types = assembly.GetTypes();
  3. MethodInfo[] methods = null;
  4. foreach(Type t in types)
  5. {
  6.      if(t.Name == "MyClass")
  7.      {    
  8.             methods = type.GetMethods();
  9.      }
  10. }
  11.  
This code gets all methods in the assembly. Is there a way to get only user defined methods?

Thanks in advance
Feb 12 '09 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 750
PRR
try this
Expand|Select|Wrap|Line Numbers
  1. Assembly assembly = Assembly.LoadFrom(path);
  2.                 Type[] types = assembly.GetTypes();
  3.                 MethodInfo[] methods = null;
  4.                 List<MethodInfo> classA=new List<MethodInfo>() ;
  5.                 Type tt=null;
  6. foreach (Type t in types)
  7.                 {
  8.                     if (t.Name == "ClassName")
  9.                     {
  10.                         tt=t;
  11.                         methods = t.GetMethods();
  12.                     }
  13.                 }
  14.  
  15.                 for(int i=0;i<methods.Length;i++)
  16.                 {
  17.                     if(methods[i].DeclaringType==tt)
  18.                     {
  19.                         classA.Add(methods[i]);
  20.  
  21.                     }
  22.                 }
  23.                 foreach (MethodInfo m in classA)
  24.                 {
  25.  
  26.                     Console.WriteLine(m.Name);
  27.                 }
  28.  
Feb 13 '09 #2

vekipeki
Expert 100+
P: 229
If you are writing the source for the loaded assembly also, you can also assign custom Attributes to your methods, and then filter them by attribute (just to distinguish between "important" and "unimportant" methods).

Example: http://msdn.microsoft.com/en-us/libr...tw(VS.80).aspx
Feb 13 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.