Solved

Reflection with unknown assembly?

Posted on 2004-10-04
5
525 Views
Last Modified: 2012-06-21
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,myParams);
...

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
0
Comment
Question by:epaga
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12217918
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.
0
 

Author Comment

by:epaga
ID: 12224146
so say I do know I need "System.IO"...how do I load that dynamically?

epaga
0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 125 total points
ID: 12224594
If you want to use a class that is defined in the System.IO assembly (e.g. FileStream) then you need to load it. You can use Type.GetType(string, bool); to load a type dynamically from an assembly.

Type fileStreamType = Type.GetType("System.IO.FileStream, System.IO");   // classname, assembly name (without .dll)
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12224601
You can also load an assemlby and enumerate all the types that it defines.
0
 

Author Comment

by:epaga
ID: 12225089
That's exactly what I wanted to know!
Thanks ptmcomp...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question