AppDomain.CreateInstanceFrom Method (String, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Creates a new instance of the specified type defined in the specified assembly file.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public ObjectHandle CreateInstanceFrom(
	string assemblyFile,
	string typeName


Type: System.String

The name, including the path, of a file that contains an assembly that defines the requested type. The assembly is loaded using the LoadFrom method.

Type: System.String

The fully qualified name of the requested type, including the namespace but not the assembly, as returned by the Type.FullName property.

Return Value

Type: System.Runtime.Remoting.ObjectHandle

An object that is a wrapper for the new instance, or null if typeName is not found. The return value needs to be unwrapped to access the real object.

Exception Condition

assemblyFile is null.


typeName is null.


assemblyFile was not found.


typeName was not found in assemblyFile.


The operation is attempted on an unloaded application domain.


No parameterless public constructor was found.


The caller does not have sufficient permission to call this constructor.


assemblyFile is not a valid assembly.


Version 2.0 or later of the common language runtime is currently loaded and assemblyFile was compiled with a later version.


An assembly or module was loaded twice with two different evidences.


This instance is null.

The default constructor for typeName is invoked.

For more information, see the Activator.CreateInstanceFrom method.

When the CreateInstanceFrom method is used to create an instance in a target application domain, other than the application domain from which the call is made, the assembly is loaded in the target application domain. However, if the instance is unwrapped in the calling application domain, using the unwrapped instance in certain ways can cause the assembly to be loaded into the calling application domain. For example, after the instance is unwrapped, its type information might be requested, in order to call its methods late-bound. When the assembly is loaded into the calling application domain, exceptions can occur.

  • If another version of the same assembly was previously loaded into the calling application domain, or if the load path of the calling application domain is different from that of the target application domain, exceptions such as MissingMethodException can occur.

  • If the calling application domain makes early-bound calls to the instance type, InvalidCastException can be thrown when an attempt is made to cast the instance.

The following example shows how to use the CreateInstanceFrom(String, String) method overload to create an instance of an object in a target application domain and call its methods.

The example defines the MarshallableExample class, which can be marshaled across application domain boundaries. The example builds a path to the currently executing assembly, creates a target application domain, and uses the CreateInstanceFrom(String, String) method overload to load the example assembly into the target application domain and create an instance of MarshallableExample.


The path is absolute in this example, but a relative path would also work because the Assembly.LoadFrom method is used to load the assembly.

After unwrapping the object handle, the example demonstrates three ways to use an object in a target application domain:

  • Invoking a method with late binding, using reflection. This requires type information, which causes the assembly to be loaded into the application domain of the caller. (In this example, it is already loaded.)

  • Casting the object to an interface known to both the caller and the callee. If the interface is defined in the calling assembly or in a third assembly referenced by both the caller and the callee, the called assembly is not loaded into the application domain of the caller.

  • Using the object directly when its type is known to the caller. The assembly must be loaded into the application domain of the caller.

Another way to avoid loading the called assembly into the application domain of the caller is for the caller to derive from the MarshalByRefObject class and to define a method that can be run in the target application domain. That method can use reflection to examine a target assembly, because the target assembly is already loaded into the target application domain. See the example for the DynamicDirectory property.

using System;

public interface ITest
    void Test(string greeting);

public class MarshallableExample : MarshalByRefObject, ITest
    static void Main()
        // Construct a path to the current assembly.
        string assemblyPath = Environment.CurrentDirectory + "\\" +
            typeof(MarshallableExample).Assembly.GetName().Name + ".exe";

        AppDomain ad = AppDomain.CreateDomain("MyDomain");

        System.Runtime.Remoting.ObjectHandle oh = 
            ad.CreateInstanceFrom(assemblyPath, "MarshallableExample");

        object obj = oh.Unwrap();

        // Three ways to use the newly created object, depending on how
        // much is known about the type: Late bound, early bound through 
        // a mutually known interface, or early binding of a known type.
            Type.DefaultBinder, obj, new object[] { "Hello" });

        ITest it = (ITest) obj;

        MarshallableExample ex = (MarshallableExample) obj;

    public void Test(string greeting)
        Console.WriteLine("{0} from '{1}'!", greeting,

/* This example produces the following output:

Hello from 'MyDomain'!
Hi from 'MyDomain'!
Goodbye from 'MyDomain'!


for the ability to access the location of the assembly. Associated enumeration: FileIOPermissionAccess.PathDiscovery


for the ability to read the file containing the assembly manifest. Associated enumeration: FileIOPermissionAccess.Read


for the ability to access the location of the assembly if the assembly is not local.

.NET Framework
Available since 1.1
Return to top