Export (0) Print
Expand All

AppDomain.CreateInstanceAndUnwrap Method (String, String)

Creates a new instance of the specified type. Parameters specify the assembly where the type is defined, and the name of the type.

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

Public Function CreateInstanceAndUnwrap ( _
	assemblyName As String, _
	typeName As String _
) As Object


Type: System.String

The display name of the assembly. See Assembly.FullName.

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.Object
An instance of the object specified by typeName.


assemblyName or typeName is Nothing.


No matching public constructor was found.


typename was not found in assemblyName.


assemblyName was not found.


The caller does not have permission to call this constructor.


The operation is attempted on an unloaded application domain.


assemblyName is not a valid assembly.


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


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

This is a convenience method that combines CreateInstance and ObjectHandle.Unwrap. This method calls the default constructor for typeName.

See AssemblyName for the format of assemblyName. See the Type.FullName property for the format of typeName.


If you make an early-bound call to a method M of an object of type T1 that was returned by CreateInstanceAndUnwrap, and that method makes an early-bound call to a method of an object of type T2 in an assembly C other than the current assembly or the assembly containing T1, assembly C is loaded into the current application domain. This loading occurs even if the early-bound call to T1.M() was made in the body of a DynamicMethod, or in other dynamically generated code. If the current domain is the default domain, assembly C cannot be unloaded until the process ends. If the current domain later attempts to load assembly C, the load might fail.

The following code example shows the simplest way to execute code in another application domain. The example defines a class named Worker that inherits from MarshalByRefObject. The Worker class defines a method that displays the name of the application domain in which it is executing. The example creates instances of Worker in the default application domain and in a new application domain.


The assembly that contains Worker must be loaded into both application domains, but it can load other assemblies that exist only in the new application domain.

Imports System.Reflection

Public Class Worker
    Inherits MarshalByRefObject

    Public Sub PrintDomain() 
        Console.WriteLine("Object is executing in AppDomain ""{0}""", _
    End Sub  
End Class  

Class Example

    Public Shared Sub Main() 
        ' Create an ordinary instance in the current AppDomain 
        Dim localWorker As New Worker()

        ' Create a new application domain, create an instance 
        ' of Worker in the application domain, and execute code 
        ' there. 
        Dim ad As AppDomain = AppDomain.CreateDomain("New domain")
        Dim remoteWorker As Worker = CType( _
            ad.CreateInstanceAndUnwrap( _
                GetType(Worker).Assembly.FullName, _
                "Worker"), _

    End Sub  
End Class  

' This code produces output similar to the following: 

'Object is executing in AppDomain "source.exe" 
'Object is executing in AppDomain "New domain"

.NET Framework

Supported in: 4.5.2, 4.5.1, 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.

© 2014 Microsoft