AppDomain.CreateInstanceFrom Method (String, String)

Updated: May 2010

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

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

public final function CreateInstanceFrom(
	assemblyFile : String, 
	typeName : String
) : ObjectHandle

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 a null reference (Nothing in Visual Basic) 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 a null reference (Nothing in Visual Basic).

-or-

typeName is a null reference (Nothing in Visual Basic).

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 a null reference (Nothing in Visual Basic).

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.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Date

History

Reason

May 2010

Added example.

Customer feedback.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft