views:

105

answers:

2

Hello,

I want to use this code to grab all methods from the assembly "Mscorlib.dll" but i get this error

"Unable to cast object of type 'System.Reflection.RuntimeConstructorInfo' to type 'System.Reflection.MethodInfo'."

Basically all I want to do is get a list of interfaces or Members of that assembly.

Heres the code:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection;

namespace ConsoleApplication1 {
    class Program
    {

        static void Main(string[] args)
        {
            Assembly a = Assembly.Load("Mscorlib.dll");
            foreach (Type oType in a.GetTypes())
            {
                MemberInfo[] f = oType.GetMembers();
                foreach (MethodInfo m in f)
                    Console.WriteLine("Member: {0}",m.ToString());
            }
         }
      } 
}

What do you think?

+1  A: 

Did you mean to use oType.Getmethods()? Because otherwise, you're enumerating across all members but expecting them to all be methods.

John Saunders
+4  A: 

Hey,

Yes, Members can't get interchanged with methods, should be:

MemberInfo[] f = oType.GetMembers(); 
foreach (MemberInfo m in f) 
     Console.WriteLine("Member: {0}",m.ToString());

Change is: foreach (MemberInfo m in f)

Brian
Hello Brian, Yes your right! Such a stupid mistake... thanks
MW