New instance from a manually loaded assembly

You need to use assembly redirection to tell the loader to load a newer (but still compatible) assembly for your application when it's binding assemblies. This is something that you do in app.config or web.config, depending on the application type. For example (taken from the link above):

    <assemblyIdentity name="someAssembly" publicKeyToken="32ab4ba45e0a69a1"
        culture="en-us" />
    <bindingRedirect oldVersion="" newVersion="" />

oldVersion allows for a version range, so multiple older versions can be mapped to a single new version of an assembly. Also, multiple bindingRedirect entries may be present.


If you want to dynamically load a type from an assembly, you cannot reference the type itself (that is, you cannot have something like Class1 obj = ...; (var obj = ... is the same) because this ties the type to the one seen by the compiler. Assembly redirection would work but if you don't know the right version, you cannot use it.

Another option is to define interfaces in your main application and then have the various types in various assemblies implement these interfaces. This way you can dynamically load a type and cast it to a known interface type. For this, you can use either dependency injection (like LightInject, etc.) or you can have a config file that lists what assemblies contain a particular implementation of an interface. At the end you'd do something like:

IInterface obj = (IInterface) Activator.CreateInstance ( "assemblyname",
    "typename" ).Unwrap ();

This assumes that assemblyname refers to a known assembly that can be loaded and that typename is in that assembly and it implements IInterface.

Using interfaces ensures that the only hardcoded type at compile time is the interface and you can load any implementation from any assembly, as long as those implementations implement the right interface. This makes it easy to swap out various components.

Using a DI library, much of this is done for you - the various libraries deal with discovering types implementing interfaces in assemblies and then they give you a new instance when you request one.

You can also look at this answer to a similar question for some extra information:


  1. Adonis

    • 2019/3/15

    You need to use assembly redirection to tell the loader to load a newer (but still compatible) assembly for your application when it's 

  2. Waylon

    • 2018/1/1

    For the new dll to be generated it requires MyApp.exe; MyApp.exe cannot be used without the new dll. maybe post what your goal is and we can help structure your app correctly. With proper responsibility delegated MyApp.exe should be making the new generated assembly do work not require MyApp.exe to use objects from the new dll.

  3. Silvestri

    • 2021/4/8

    Yes. You need to use Assembly.LoadFrom to load the assembly into memory, then you can use Activator.CreateInstance to create an instance of 

  4. Walter

    • 2015/10/8

    The following code loads an assembly named "example.exe" or "example.dll" into the current application domain, gets a type named Example from the assembly, gets a parameterless method named MethodA for that type, and executes the method. For a complete discussion on obtaining information from a loaded assembly, see Dynamically Loading and Using

  5. Jagger

    • 2020/1/22

    The reflection-only context is new in the .NET Framework version 2.0. Methods such as CreateInstance and CreateInstanceAndUnwrap of the 

  6. Marcelo

    • 2020/3/6

    CreateInstance (String, Boolean, BindingFlags, Binder, Object [], CultureInfo, Object []) Locates the specified type from this assembly and creates an instance of it using the system activator, with optional case-sensitive search and having the specified culture, arguments, and binding and activation attributes.

  7. Santos

    • 2016/8/26

    The second assembly, DecoupledAssembly, implements a class named The problem is that you cannot create an instance of an Interface.

  8. Lucas

    • 2016/3/9

    Now when the developers use the newdomain.CreateIntance method, returns a wrapped instance of the assembly from the new appdomain. This not only loads the assembly into a new appdomain but also loads the assembly into the current appdomain.

  9. Hayes

    • 2021/4/13

    NewInstance fills in default values for various optional hooks and packs the constructor arguments into an array. Without relying on such helpers, the object 

  10. Zane

    • 2015/6/20

    See if the assembly is already loaded. If a different version is loaded, a FileLoadException will be thrown, unless it’s a strongly-named assembly that can be loaded in several versions side-by-side. If it’s a strongly-named assembly, check the Global Assembly Cache (GAC). The GAC is a place on the machine to share assemblies for multiple

  11. Martinelli

    • 2016/8/28

    I have a assembly. In this assembly I have a class and interface. I need to load this assembly at runtime and want to create an object of the class and also 

  12. Jerome

    • 2020/12/18

    Load (Byte []) Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller. Load (String) Loads an assembly with the specified name. Load (AssemblyName) Loads an assembly given its AssemblyName.

  13. Moshe

    • 2020/9/16

    To achieve this, I've tried to implement a composition root concept, where all implementation must have an instance of ICompositionRootComposer to register 

  14. Sala

    • 2018/11/6

    This section discusses how to create and set up Assembly Definition and Assembly Reference assets to define assemblies for your project: Defining assemblies 

  15. Langston

    • 2020/12/3

    The last 2 lines cast the new instance to the correct interface and step was to manually load the assembly by hard-coding the value.

  16. Tristan

    • 2015/6/16

    NET assembly and the assembly should load into the application just Upon startup, Form1_Load will create an instance of proxy with the 

Comments are closed.

Recent Posts