Export (0) Print
Expand All

AppDomain.CreateInstanceFrom Method (String, String)

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
)

Parameters

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

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

Implements

_AppDomain.CreateInstanceFrom(String, String)

ExceptionCondition
ArgumentNullException

assemblyFile is null.

-or-

typeName is null.

FileNotFoundException

assemblyFile was not found.

TypeLoadException

typeName was not found in assemblyFile.

AppDomainUnloadedException

The operation is attempted on an unloaded application domain.

MissingMethodException

No parameterless public constructor was found.

MethodAccessException

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

BadImageFormatException

assemblyFile is not a valid assembly.

-or-

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

FileLoadException

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

NullReferenceException

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.

NoteNote

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. 
        //
        obj.GetType().InvokeMember("Test", 
            System.Reflection.BindingFlags.InvokeMethod, 
            Type.DefaultBinder, obj, new object[] { "Hello" });

        ITest it = (ITest) obj;
        it.Test("Hi");

        MarshallableExample ex = (MarshallableExample) obj;
        ex.Test("Goodbye");
    }

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

/* This example produces the following output:

Hello from 'MyDomain'!
Hi from 'MyDomain'!
Goodbye from 'MyDomain'!
 */

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft