?
Solved

Reflection with unknown assembly?

Posted on 2004-10-04
5
Medium Priority
?
530 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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