Solved

Reflection with unknown assembly?

Posted on 2004-10-04
5
527 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 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

Technology Partners: 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: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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