tags:

views:

6658

answers:

9

Hi

Is it possible to instantiate a object at runtime if I only have the DLL name and the class name, without adding a reference to your project ?

eg. DLL name : library.dll and class name : Company.Project.Classname

The class implements a interface, so once I instantiate the class, I will then cast it to the interface.

EDITED :

I dont have the absolute path of the dll, so assemlby.LoadFile ect. wont work. The dll might be in the APP root, system32 or even loaded in the GAC. I only have the dll name.

+4  A: 

Activator.CreateInstance ought to work.

IFace object = (IFace)Activator.CreateInstance( "AssemblyName",
                                                "TypeName",
                                                null );
tvanfosson
+1  A: 

Yes. I don't have any examples that I've done personally available right now. I'll post later when I find some. Basically you'll use reflection to load the assembly and then to pull whatever types you need for it.

In the meantime, this link should get you started:

Using reflection to load unreferenced assemblies at runtime

Giovanni Galbo
+1  A: 

Yes, it is, you will want to use the static Load method on the Assembly class, and then call then call the CreateInstance method on the Assembly instance returned to you from the call to Load.

Also, you can call one of the other static methods starting with "Load" on the Assembly class, depending on your needs.

casperOne
+1  A: 
((ISomeInterface)Activator.CreateInstance(Assembly.LoadFile("somePath").GetTypes()[0])).SomeInterfaceMethod();
abatishchev
+21  A: 

Yes. You need to use Assembly.LoadFrom to load the assembly into memory, then you can use Activator.CreateInstance to create an instance of your preferred type. You'll need to look the type up first using reflection. Here is a simple example:

Assembly assembly = Assembly.LoadFrom("MyNice.dll");

Type type = assembly.GetType("MyType");

object instanceOfMyType = Activator.CreateInstance(type);

Update

When you have the assembly file name and the type name, you can use Activator.CreateInstance(assmblyFileName, typeName) to ask the .NET type resolution to resolve that into a type. You could wrap that with a try/catch so that if it fails, you can perform a search of directories where you may specifically store additional assemblies that otherwise might not be searched. This would use the preceding method at that point.

Jeff Yates
I dont have the absolute path of the dll, so assemlby.LoadFile ect. wont work, any other ideas ?
MegaByte
This answer helped me immensely. Thank you!
rp
@rp Always happy to help (and only a year late in saying so!)
Jeff Yates
+1  A: 

Depending how intrinsic this kind of functionality is to your project, you might want to consider something like MEF which will take care of the loading and tying together of components for you.

HTH, Kent

Kent Boogaart
+1  A: 

You can load an assembly using *Assembly.Load** methods. Using Activator.CreateInstance you can create new instances of the type you want. Keep in mind that you have to use the full type name of the class you want to load (for example Namespace.SubNamespace.ClassName). Using the method InvokeMember of the Type class you can invoke methods on the type.

Also, take into account that once loaded, an assembly cannot be unloaded until the whole AppDomain is unloaded too (this is basically a memory leak).

Dario Solera
+6  A: 

Consider the limitations of the different Load* methods. From the MSDN docs...

LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does.

More information on Load Contexts can be found in the LoadFrom docs.

Anthony Mastrean
+1  A: 

You can do this things on this way....

using System.Reflection;

Assembly MyDALL = Assembly.Load("DALL"); //DALL name of your assembly Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // name of your class object obj = Activator.CreateInstance(MyLoadClass);

Pankaj