Solved

Reflection with unknown assembly?

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now