AppDomain.CreateInstanceFrom Method (String, String)
Creates a new instance of the specified type defined in the specified assembly file.
Assembly: mscorlib (in mscorlib.dll)
abstract CreateInstanceFrom : assemblyFile:string * typeName:string -> ObjectHandle override CreateInstanceFrom : assemblyFile:string * typeName:string -> ObjectHandle
- 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.
Return ValueType: 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.
assemblyFile is a null reference (Nothing in Visual Basic).
typeName is a null reference (Nothing in Visual Basic).
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 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 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 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.
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.