C# reflection help please...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackirish
    New Member
    • Sep 2008
    • 22

    #1

    C# reflection help please...

    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;

    Code:
    Assembly assembly = Assembly.LoadFile(assemblyName);
    Type[] types = assembly.GetTypes();
    MethodInfo[] methods = null;
    foreach(Type t in types)
    {
         if(t.Name == "MyClass")
         {    
                methods = type.GetMethods();
         }
    }
    This code gets all methods in the assembly. Is there a way to get only user defined methods?

    Thanks in advance
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    try this
    Code:
    Assembly assembly = Assembly.LoadFrom(path);
                    Type[] types = assembly.GetTypes();
                    MethodInfo[] methods = null;
                    List<MethodInfo> classA=new List<MethodInfo>() ;
                    Type tt=null;
    foreach (Type t in types)
                    {
                        if (t.Name == "ClassName")
                        {
                            tt=t;
                            methods = t.GetMethods();
                        }
                    }
                    
                    for(int i=0;i<methods.Length;i++)
                    {
                        if(methods[i].DeclaringType==tt)
                        {
                            classA.Add(methods[i]);
    
                        }
                    }
                    foreach (MethodInfo m in classA)
                    {
    
                        Console.WriteLine(m.Name);
                    }

    Comment

    • vekipeki
      Recognized Expert New Member
      • Nov 2007
      • 229

      #3
      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 "unimportan t" methods).

      Example: http://msdn.microsoft.com/en-us/libr...tw(VS.80).aspx

      Comment

      Working...