Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

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.

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.

I don't have the absolute path of the dll, so assemlby. LoadFile ect. Won't work, any other ideas?

– MegaByte Jan 22 '09 at 6:27 This answer helped me immensely. Thank you! – rp.

Mar 4 '09 at 6:33 @rp Always happy to help (and only a year late in saying so! ) – Jeff Yates Feb 16 '10 at 14:33 @MegaByte: LoadFrom is different to LoadFile. It will resolve your dependencies and it should resolve the DLL name from known paths (GAC, exe directory, etc.) See MSDN for more information.

– Jeff Yates Jan 18 at 13:54 This helps... mark it as accepted answer? – Cipi Feb 14 at 8:21.

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.

Excellent catch on the differences here, thank you! – benr Aug 1 at 15:55.

Activator. CreateInstance ought to work. IFace object = (IFace)Activator.

CreateInstance( "AssemblyName", "TypeName" ) .Unwrap(); Note: The type name must be the fully qualified type. Example: var aray = (IList)Activator. CreateInstance("mscorlib","System.Collections.

ArrayList").Unwrap(); aray. Add(10); foreach (object obj in aray) { Console. WriteLine(obj); }.

Just a note on this: TypeName must be fully qualified. I had to call this like: Activator. CreateInstance("MyAssembly","MyAssembly.

TypeName") And that returns an ObjectHandle. To get down to your interface you need to do ObjectHandle.UnWrap() – Anthony Sottile Aug 8 at 21:29 @Anthony -- I've updated. – tvanfosson Aug 8 at 21:56.

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.

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.

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.

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).

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).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions