epaga
asked on
Reflection with unknown assembly?
Hi there!
I'm working with System.Reflection. So far it works nicely for dll's, like to invoke a static method:
Assembly assembly = Assembly.LoadFrom(dllName) ;
System.Type type = assembly.getType(className );
MethodInfo method = type.GetMethod(mName,types ); // types are the types of parameters I'm using
Object myObj = method.Invoke(null,myParam s);
...
What do I do though, if I want to call a method of a class not in that dll, but say, for example, in System.IO? That is, I'm calling this reflection method from a different program, and I can specify that I want to use method "getFiles()" in class "Directory" or even class "System.IO.Directory". But of course I don't specify the assembly to use.
Is there a way to do that? Hope my question is clear, if not, please ask...
Thanks!
epaga
I'm working with System.Reflection. So far it works nicely for dll's, like to invoke a static method:
Assembly assembly = Assembly.LoadFrom(dllName)
System.Type type = assembly.getType(className
MethodInfo method = type.GetMethod(mName,types
Object myObj = method.Invoke(null,myParam
...
What do I do though, if I want to call a method of a class not in that dll, but say, for example, in System.IO? That is, I'm calling this reflection method from a different program, and I can specify that I want to use method "getFiles()" in class "Directory" or even class "System.IO.Directory". But of course I don't specify the assembly to use.
Is there a way to do that? Hope my question is clear, if not, please ask...
Thanks!
epaga
It is definitely possible since there is a program called ".net Reflector" from Lutz Roeder which is doing it. But you must know where your class is located / what assemblies you need to load to create the class. You could of course load a set of assemblies by default but if the class is defined in a different assembly that particular assembly and its dependencies must be loaded.
ASKER
so say I do know I need "System.IO"...how do I load that dynamically?
epaga
epaga
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can also load an assemlby and enumerate all the types that it defines.
ASKER
That's exactly what I wanted to know!
Thanks ptmcomp...
Thanks ptmcomp...