CreateInstanceAndUnwrap Method (String, String)

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 Object CreateInstanceAndUnwrap (
	string assemblyName,
	string typeName
)
public Object CreateInstanceAndUnwrap (
	String assemblyName, 
	String typeName
)
public function CreateInstanceAndUnwrap (
	assemblyName : String, 
	typeName : String
) : Object
Not applicable.

Parameters

assemblyName

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

typeName

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

Return Value

An instance of the object specified by typeName.

Exception typeCondition

ArgumentNullException

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

MissingMethodException

No matching public constructor was found.

TypeLoadException

typename was not found in assemblyName.

FileNotFoundException

assemblyName was not found.

MethodAccessException

The caller does not have permission to call this constructor.

AppDomainUnloadedException

The operation is attempted on an unloaded application domain.

BadImageFormatException

assemblyName is not a valid assembly.

-or-

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

FileLoadException

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.

NoteNote:

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.

NoteNote:

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.

using System;
using System.Reflection;
 
public class Worker : MarshalByRefObject
{
    public void PrintDomain() 
    { 
        Console.WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain.CurrentDomain.FriendlyName); 
    }
}
 
class Example
{
    public static void Main()
    {
        // Create an ordinary instance in the current AppDomain
        Worker localWorker = new Worker();
        localWorker.PrintDomain();
 
        // Create a new application domain, create an instance
        // of Worker in the application domain, and execute code
        // there.
        AppDomain ad = AppDomain.CreateDomain("New domain");
        Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
            Assembly.GetExecutingAssembly().FullName,
            "Worker");
        remoteWorker.PrintDomain();
    }
}

/* This code produces output similar to the following:

Object is executing in AppDomain "source.exe"
Object is executing in AppDomain "New domain"
 */

  • FileIOPermissionAccess  for the ability to access the location of the assembly if code base is supplied by a configuration file. Associated enumeration: FileIOPermissionAccess.PathDiscovery
  • FileIOPermissionAccess  for the ability to read the file containing the assembly manifest, or if you are creating a type from a module other than the manifest file. Associated enumeration: FileIOPermissionAccess.Read
  • WebPermission  for the ability to access the location of the assembly if the assembly is not local.

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft