AppDomain.CreateInstanceAndUnwrap 方法

定义

创建指定类型的新实例。

重载

CreateInstanceAndUnwrap(String, String)

创建指定类型的新实例。 参数指定在其中定义类型的程序集以及类型的名称。

CreateInstanceAndUnwrap(String, String, Object[])

创建指定类型的新实例。 参数指定在其中定义类型的程序集、类型的名称和激活属性数组。

CreateInstanceAndUnwrap(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

创建指定程序集中定义的指定类型的新实例,指定是否忽略类型名称大小写;用于选择要创建的类型的绑定属性和绑定器;构造函数的参数;区域性;和激活属性。

CreateInstanceAndUnwrap(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已过时.

创建指定类型的新实例。 参数指定类型的名称及其创建方式。

CreateInstanceAndUnwrap(String, String)

Source:
AppDomain.cs
Source:
AppDomain.cs
Source:
AppDomain.cs

创建指定类型的新实例。 参数指定在其中定义类型的程序集以及类型的名称。

public:
 System::Object ^ CreateInstanceAndUnwrap(System::String ^ assemblyName, System::String ^ typeName);
public object? CreateInstanceAndUnwrap (string assemblyName, string typeName);
public object CreateInstanceAndUnwrap (string assemblyName, string typeName);
member this.CreateInstanceAndUnwrap : string * string -> obj
Public Function CreateInstanceAndUnwrap (assemblyName As String, typeName As String) As Object

参数

assemblyName
String

程序集的显示名称。 请参阅 FullName

typeName
String

请求类型的完全限定名称,包括命名空间而不是程序集,由 FullName 属性返回。

返回

typeName指定的对象的实例。

例外

assemblyNametypeNamenull

找不到匹配的公共构造函数。

assemblyName中找不到 typeName

找不到 assemblyName

调用方无权调用此构造函数。

在卸载的应用程序域中尝试此操作。

assemblyName 当前加载的运行时不是有效的程序集。

程序集或模块加载了两次,其中包含两个不同的证据。

示例

下面的代码示例演示了在另一个应用程序域中执行代码的最简单方法。 该示例定义一个名为 Worker 的类,该类继承自 MarshalByRefObjectWorker 类定义一个方法,用于显示在其中执行应用程序域的名称。 该示例在默认应用程序域和新应用程序域中创建 Worker 实例。

注意

包含 Worker 的程序集必须加载到这两个应用程序域中,但它可以加载仅存在于新应用程序域中的其他程序集。

using namespace System;
using namespace System::Reflection;

public ref class Worker : MarshalByRefObject
{
public:
    void PrintDomain() 
    { 
        Console::WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain::CurrentDomain->FriendlyName); 
    }
};
 
void main()
{
    // Create an ordinary instance in the current AppDomain
    Worker^ localWorker = gcnew 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(
        Worker::typeid->Assembly->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"
 */
using System;
using System.Reflection;

public class CreateInstanceWorker : MarshalByRefObject
{
    public void PrintDomain()
    {
        Console.WriteLine("Object is executing in AppDomain \"{0}\"",
            AppDomain.CurrentDomain.FriendlyName);
    }
}

class CreateInstanceAndUnwrapSourceSnippet
{
    public static void Main()
    {
        // Create an ordinary instance in the current AppDomain
        CreateInstanceWorker localWorker = new CreateInstanceWorker();
        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");
        CreateInstanceWorker remoteWorker = (CreateInstanceWorker) ad.CreateInstanceAndUnwrap(
            typeof(CreateInstanceWorker).Assembly.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"
 */
open System
open System.Reflection

type Worker() =
    inherit MarshalByRefObject()
    member _.PrintDomain() =
        printfn $"Object is executing in AppDomain \"{AppDomain.CurrentDomain.FriendlyName}\""

// Create an ordinary instance in the current AppDomain
let localWorker = Worker()
localWorker.PrintDomain()

// Create a new application domain, create an instance
// of Worker in the application domain, and execute code
// there.
let ad = AppDomain.CreateDomain "New domain"
let remoteWorker = 
    ad.CreateInstanceAndUnwrap(typeof<Worker>.Assembly.FullName, "Worker") :?> 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"
Imports System.Reflection

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

Class Example
    
    Public Shared Sub Main() 
        ' Create an ordinary instance in the current AppDomain
        Dim localWorker As New Worker()
        localWorker.PrintDomain()
        
        ' 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"), _
            Worker)
        remoteWorker.PrintDomain()
    
    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"

注解

这是一种将 CreateInstanceObjectHandle.Unwrap相结合的便利方法。 此方法调用 typeName的无参数构造函数。

有关 assemblyName格式,请参阅 AssemblyName。 有关 typeName格式,请参阅 Type.FullName 属性。

注意

如果对 CreateInstanceAndUnwrap返回的 T1 类型的对象的方法 M 进行早期绑定调用,并且该方法对程序集中类型 T2 对象的方法进行早期绑定调用,C 当前程序集或包含 T1的程序集,则程序集 C 将加载到当前应用程序域中。 即使对 T1.M() 的早期绑定调用是在 DynamicMethod的正文或其他动态生成的代码中进行的,也会发生此加载。 如果当前域是默认域,则在进程结束之前,无法卸载程序集 C。 如果当前域稍后尝试加载程序集 C,则加载可能会失败。

另请参阅

适用于

CreateInstanceAndUnwrap(String, String, Object[])

Source:
AppDomain.cs
Source:
AppDomain.cs
Source:
AppDomain.cs

创建指定类型的新实例。 参数指定在其中定义类型的程序集、类型的名称和激活属性数组。

public:
 System::Object ^ CreateInstanceAndUnwrap(System::String ^ assemblyName, System::String ^ typeName, cli::array <System::Object ^> ^ activationAttributes);
public object? CreateInstanceAndUnwrap (string assemblyName, string typeName, object?[]? activationAttributes);
public object CreateInstanceAndUnwrap (string assemblyName, string typeName, object[] activationAttributes);
member this.CreateInstanceAndUnwrap : string * string * obj[] -> obj
Public Function CreateInstanceAndUnwrap (assemblyName As String, typeName As String, activationAttributes As Object()) As Object

参数

assemblyName
String

程序集的显示名称。 请参阅 FullName

typeName
String

请求类型的完全限定名称,包括命名空间而不是程序集,由 FullName 属性返回。

activationAttributes
Object[]

可以参与激活的一个或多个属性的数组。 通常,包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。

此参数与客户端激活的对象相关。客户端激活是保留的旧技术,用于向后兼容,但不建议用于新开发。 分布式应用程序应改用 Windows Communication Foundation。

返回

typeName指定的对象的实例。

例外

assemblyNametypeNamenull

找不到匹配的公共构造函数。

assemblyName中找不到 typeName

找不到 assemblyName

调用方无权调用此构造函数。

调用方无法为不继承自 MarshalByRefObject的对象提供激活属性。

在卸载的应用程序域中尝试此操作。

assemblyName 当前加载的运行时不是有效的程序集。

程序集或模块加载了两次,其中包含两个不同的证据。

示例

using namespace System;
using namespace System::IO;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::Remoting;

ref class ADDyno
{
public:
   static Type^ CreateADynamicAssembly( interior_ptr<AppDomain^> myNewDomain, String^ executableNameNoExe )
   {
      String^ executableName = String::Concat( executableNameNoExe, ".exe" );
      AssemblyName^ myAsmName = gcnew AssemblyName;
      myAsmName->Name = executableNameNoExe;
      myAsmName->CodeBase = Environment::CurrentDirectory;
      AssemblyBuilder^ myAsmBuilder = ( *myNewDomain)->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::RunAndSave );
      Console::WriteLine( "-- Dynamic Assembly instantiated." );
      ModuleBuilder^ myModBuilder = myAsmBuilder->DefineDynamicModule( executableNameNoExe, executableName );
      TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( executableNameNoExe, TypeAttributes::Public, MarshalByRefObject::typeid );
      array<Type^>^temp0 = nullptr;
      MethodBuilder^ myFCMethod = myTypeBuilder->DefineMethod( "CountLocalFiles", static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Static), nullptr, temp0 );
      MethodInfo^ currentDirGetMI = Environment::typeid->GetProperty( "CurrentDirectory" )->GetGetMethod();
      array<Type^>^temp1 = {String::typeid};
      MethodInfo^ writeLine0objMI = Console::typeid->GetMethod( "WriteLine", temp1 );
      array<Type^>^temp2 = {String::typeid,Object::typeid,Object::typeid};
      MethodInfo^ writeLine2objMI = Console::typeid->GetMethod( "WriteLine", temp2 );
      array<Type^>^temp3 = {String::typeid};
      MethodInfo^ getFilesMI = Directory::typeid->GetMethod( "GetFiles", temp3 );
      myFCMethod->InitLocals = true;
      ILGenerator^ myFCIL = myFCMethod->GetILGenerator();
      Console::WriteLine( "-- Generating MSIL method body..." );
      LocalBuilder^ v0 = myFCIL->DeclareLocal( String::typeid );
      LocalBuilder^ v1 = myFCIL->DeclareLocal( int::typeid );
      LocalBuilder^ v2 = myFCIL->DeclareLocal( String::typeid );
      LocalBuilder^ v3 = myFCIL->DeclareLocal( array<String^>::typeid );
      Label evalForEachLabel = myFCIL->DefineLabel();
      Label topOfForEachLabel = myFCIL->DefineLabel();

      // Build the method body.
      myFCIL->EmitCall( OpCodes::Call, currentDirGetMI, nullptr );
      myFCIL->Emit( OpCodes::Stloc_S, v0 );
      myFCIL->Emit( OpCodes::Ldc_I4_0 );
      myFCIL->Emit( OpCodes::Stloc_S, v1 );
      myFCIL->Emit( OpCodes::Ldstr, "---" );
      myFCIL->EmitCall( OpCodes::Call, writeLine0objMI, nullptr );
      myFCIL->Emit( OpCodes::Ldloc_S, v0 );
      myFCIL->EmitCall( OpCodes::Call, getFilesMI, nullptr );
      myFCIL->Emit( OpCodes::Stloc_S, v3 );
      myFCIL->Emit( OpCodes::Br_S, evalForEachLabel );

      // foreach loop starts here.
      myFCIL->MarkLabel( topOfForEachLabel );

      // Load array of strings and index, store value at index for output.
      myFCIL->Emit( OpCodes::Ldloc_S, v3 );
      myFCIL->Emit( OpCodes::Ldloc_S, v1 );
      myFCIL->Emit( OpCodes::Ldelem_Ref );
      myFCIL->Emit( OpCodes::Stloc_S, v2 );
      myFCIL->Emit( OpCodes::Ldloc_S, v2 );
      myFCIL->EmitCall( OpCodes::Call, writeLine0objMI, nullptr );

      // Increment counter by one.
      myFCIL->Emit( OpCodes::Ldloc_S, v1 );
      myFCIL->Emit( OpCodes::Ldc_I4_1 );
      myFCIL->Emit( OpCodes::Add );
      myFCIL->Emit( OpCodes::Stloc_S, v1 );

      // Determine if end of file list array has been reached.
      myFCIL->MarkLabel( evalForEachLabel );
      myFCIL->Emit( OpCodes::Ldloc_S, v1 );
      myFCIL->Emit( OpCodes::Ldloc_S, v3 );
      myFCIL->Emit( OpCodes::Ldlen );
      myFCIL->Emit( OpCodes::Conv_I4 );
      myFCIL->Emit( OpCodes::Blt_S, topOfForEachLabel );

      //foreach loop end here.
      myFCIL->Emit( OpCodes::Ldstr, "---" );
      myFCIL->EmitCall( OpCodes::Call, writeLine0objMI, nullptr );
      myFCIL->Emit( OpCodes::Ldstr, "There are {0} files in {1}." );
      myFCIL->Emit( OpCodes::Ldloc_S, v1 );
      myFCIL->Emit( OpCodes::Box, int::typeid );
      myFCIL->Emit( OpCodes::Ldloc_S, v0 );
      myFCIL->EmitCall( OpCodes::Call, writeLine2objMI, nullptr );
      myFCIL->Emit( OpCodes::Ret );
      Type^ myType = myTypeBuilder->CreateType();
      myAsmBuilder->SetEntryPoint( myFCMethod );
      myAsmBuilder->Save( executableName );
      Console::WriteLine( "-- Method generated, type completed, and assembly saved to disk." );
      return myType;
   }
};

int main()
{
   String^ domainDir;
   String^ executableName = nullptr;
   Console::Write( "Enter a name for the file counting assembly: " );
   String^ executableNameNoExe = Console::ReadLine();
   executableName = String::Concat( executableNameNoExe, ".exe" );
   Console::WriteLine( "---" );
   domainDir = Environment::CurrentDirectory;
   AppDomain^ curDomain = Thread::GetDomain();

   // Create a new AppDomain, with the current directory as the base.
   Console::WriteLine( "Current Directory: {0}", Environment::CurrentDirectory );
   AppDomainSetup^ mySetupInfo = gcnew AppDomainSetup;
   mySetupInfo->ApplicationBase = domainDir;
   mySetupInfo->ApplicationName = executableNameNoExe;
   mySetupInfo->LoaderOptimization = LoaderOptimization::SingleDomain;
   AppDomain^ myDomain = AppDomain::CreateDomain( executableNameNoExe, nullptr, mySetupInfo );
   Console::WriteLine( "Creating a new AppDomain '{0}'...", executableNameNoExe );
   Console::WriteLine( "-- Base Directory = '{0}'", myDomain->BaseDirectory );
   Console::WriteLine( "-- Shadow Copy? = '{0}'", myDomain->ShadowCopyFiles );
   Console::WriteLine( "---" );
   Type^ myFCType = ADDyno::CreateADynamicAssembly(  &curDomain, executableNameNoExe );
   Console::WriteLine( "Loading '{0}' from '{1}'...", executableName, myDomain->BaseDirectory );
   BindingFlags bFlags = static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::CreateInstance | BindingFlags::Instance);
   Object^ myObjInstance = myDomain->CreateInstanceAndUnwrap( executableNameNoExe, executableNameNoExe, false, bFlags, nullptr, nullptr, nullptr, nullptr, nullptr );
   Console::WriteLine( "Executing method 'CountLocalFiles' in {0}...", myObjInstance );
   array<Object^>^temp4 = nullptr;
   myFCType->InvokeMember( "CountLocalFiles", BindingFlags::InvokeMethod, nullptr, myObjInstance, temp4 );
}
using System;
using System.IO;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Remoting;

class ADDyno
{
   public static Type CreateADynamicAssembly(ref AppDomain myNewDomain,
                         string executableNameNoExe)
   {
    string executableName = executableNameNoExe + ".exe";

    AssemblyName myAsmName = new AssemblyName();
    myAsmName.Name = executableNameNoExe;
    myAsmName.CodeBase = Environment.CurrentDirectory;

    AssemblyBuilder myAsmBuilder = myNewDomain.DefineDynamicAssembly(myAsmName,
                        AssemblyBuilderAccess.RunAndSave);
    Console.WriteLine("-- Dynamic Assembly instantiated.");

    ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule(executableNameNoExe,
                                      executableName);

    TypeBuilder myTypeBuilder = myModBuilder.DefineType(executableNameNoExe,
                        TypeAttributes.Public,
                        typeof(MarshalByRefObject));

    MethodBuilder myFCMethod = myTypeBuilder.DefineMethod("CountLocalFiles",
                        MethodAttributes.Public |
                        MethodAttributes.Static,
                        null,
                        new Type[] {  });

    MethodInfo currentDirGetMI = typeof(Environment).GetProperty("CurrentDirectory").GetGetMethod();
    MethodInfo writeLine0objMI = typeof(Console).GetMethod("WriteLine",
                     new Type[] { typeof(string) });
    MethodInfo writeLine2objMI = typeof(Console).GetMethod("WriteLine",
                     new Type[] { typeof(string), typeof(object), typeof(object) });
    MethodInfo getFilesMI = typeof(Directory).GetMethod("GetFiles",
                new Type[] { typeof(string) });

    myFCMethod.InitLocals = true;

    ILGenerator myFCIL = myFCMethod.GetILGenerator();

    Console.WriteLine("-- Generating MSIL method body...");
    LocalBuilder v0 = myFCIL.DeclareLocal(typeof(string));
    LocalBuilder v1 = myFCIL.DeclareLocal(typeof(int));
    LocalBuilder v2 = myFCIL.DeclareLocal(typeof(string));
    LocalBuilder v3 = myFCIL.DeclareLocal(typeof(string[]));

    Label evalForEachLabel = myFCIL.DefineLabel();
    Label topOfForEachLabel = myFCIL.DefineLabel();

    // Build the method body.

    myFCIL.EmitCall(OpCodes.Call, currentDirGetMI, null);
    myFCIL.Emit(OpCodes.Stloc_S, v0);
    myFCIL.Emit(OpCodes.Ldc_I4_0);
    myFCIL.Emit(OpCodes.Stloc_S, v1);
    myFCIL.Emit(OpCodes.Ldstr, "---");
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null);
    myFCIL.Emit(OpCodes.Ldloc_S, v0);
    myFCIL.EmitCall(OpCodes.Call, getFilesMI, null);
    myFCIL.Emit(OpCodes.Stloc_S, v3);

    myFCIL.Emit(OpCodes.Br_S, evalForEachLabel);

    // foreach loop starts here.
    myFCIL.MarkLabel(topOfForEachLabel);
    
        // Load array of strings and index, store value at index for output.
    myFCIL.Emit(OpCodes.Ldloc_S, v3);
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Ldelem_Ref);
    myFCIL.Emit(OpCodes.Stloc_S, v2);

    myFCIL.Emit(OpCodes.Ldloc_S, v2);
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null);

    // Increment counter by one.
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Ldc_I4_1);
    myFCIL.Emit(OpCodes.Add);
    myFCIL.Emit(OpCodes.Stloc_S, v1);

    // Determine if end of file list array has been reached.
    myFCIL.MarkLabel(evalForEachLabel);
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Ldloc_S, v3);
    myFCIL.Emit(OpCodes.Ldlen);
    myFCIL.Emit(OpCodes.Conv_I4);
    myFCIL.Emit(OpCodes.Blt_S, topOfForEachLabel);
    //foreach loop end here.

    myFCIL.Emit(OpCodes.Ldstr, "---");
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null);
    myFCIL.Emit(OpCodes.Ldstr, "There are {0} files in {1}.");
    myFCIL.Emit(OpCodes.Ldloc_S, v1);
    myFCIL.Emit(OpCodes.Box, typeof(int));
    myFCIL.Emit(OpCodes.Ldloc_S, v0);
    myFCIL.EmitCall(OpCodes.Call, writeLine2objMI, null);

    myFCIL.Emit(OpCodes.Ret);

    Type myType = myTypeBuilder.CreateType();

    myAsmBuilder.SetEntryPoint(myFCMethod);
    myAsmBuilder.Save(executableName);		
    Console.WriteLine("-- Method generated, type completed, and assembly saved to disk.");

    return myType;
   }

   public static void Main()
   {

    string domainDir, executableName = null;
    
    Console.Write("Enter a name for the file counting assembly: ");
    string executableNameNoExe = Console.ReadLine();
    executableName = executableNameNoExe + ".exe";
    Console.WriteLine("---");

    domainDir = Environment.CurrentDirectory;

    AppDomain curDomain = Thread.GetDomain();	

    // Create a new AppDomain, with the current directory as the base.

    Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory);
    AppDomainSetup mySetupInfo = new AppDomainSetup();
    mySetupInfo.ApplicationBase = domainDir;
    mySetupInfo.ApplicationName = executableNameNoExe;
    mySetupInfo.LoaderOptimization = LoaderOptimization.SingleDomain;

    AppDomain myDomain = AppDomain.CreateDomain(executableNameNoExe,
                    null, mySetupInfo);

    Console.WriteLine("Creating a new AppDomain '{0}'...",
                    executableNameNoExe);

    Console.WriteLine("-- Base Directory = '{0}'", myDomain.BaseDirectory);
    Console.WriteLine("-- Shadow Copy? = '{0}'", myDomain.ShadowCopyFiles);

    Console.WriteLine("---");
    Type myFCType = CreateADynamicAssembly(ref curDomain,
                     executableNameNoExe);

    Console.WriteLine("Loading '{0}' from '{1}'...", executableName,
              myDomain.BaseDirectory.ToString());

    BindingFlags bFlags = (BindingFlags.Public | BindingFlags.CreateInstance |
                   BindingFlags.Instance);

    Object myObjInstance = myDomain.CreateInstanceAndUnwrap(executableNameNoExe,
                executableNameNoExe, false, bFlags,
                null, null, null, null, null);

    Console.WriteLine("Executing method 'CountLocalFiles' in {0}...",
               myObjInstance.ToString());

    myFCType.InvokeMember("CountLocalFiles", BindingFlags.InvokeMethod, null,
                myObjInstance, new object[] { });
   }
}
open System
open System.IO
open System.Threading
open System.Reflection
open System.Reflection.Emit

let createADynamicAssembly (myNewDomain: byref<AppDomain>) executableNameNoExe =
    let executableName = executableNameNoExe + ".exe"

    let myAsmName = AssemblyName()
    myAsmName.Name <- executableNameNoExe
    myAsmName.CodeBase <- Environment.CurrentDirectory

    let myAsmBuilder = 
        myNewDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
    printfn "-- Dynamic Assembly instantiated."

    let myModBuilder = 
        myAsmBuilder.DefineDynamicModule(executableNameNoExe, executableName)

    let myTypeBuilder = 
        myModBuilder.DefineType(executableNameNoExe,
                        TypeAttributes.Public,
                        typeof<MarshalByRefObject>)

    let myFCMethod = 
        myTypeBuilder.DefineMethod("CountLocalFiles",
                        MethodAttributes.Public |||
                        MethodAttributes.Static,
                        null,
                        [||])

    let currentDirGetMI = typeof<Environment>.GetProperty("CurrentDirectory").GetGetMethod()
    let writeLine0objMI = typeof<Console>.GetMethod("WriteLine", [| typeof<string> |])
    let writeLine2objMI = typeof<Console>.GetMethod("WriteLine", [| typeof<string>; typeof<obj>; typeof<obj> |])
    let getFilesMI = typeof<Directory>.GetMethod("GetFiles", [| typeof<string> |])

    myFCMethod.InitLocals <- true

    let myFCIL = myFCMethod.GetILGenerator()

    printfn "-- Generating MSIL method body..."
    let v0 = myFCIL.DeclareLocal typeof<string>
    let v1 = myFCIL.DeclareLocal typeof<int>
    let v2 = myFCIL.DeclareLocal typeof<string>
    let v3 = myFCIL.DeclareLocal typeof<string[]>

    let evalForEachLabel = myFCIL.DefineLabel()
    let topOfForEachLabel = myFCIL.DefineLabel()

    // Build the method body.

    myFCIL.EmitCall(OpCodes.Call, currentDirGetMI, null)
    myFCIL.Emit(OpCodes.Stloc_S, v0)
    myFCIL.Emit(OpCodes.Ldc_I4_0)
    myFCIL.Emit(OpCodes.Stloc_S, v1)
    myFCIL.Emit(OpCodes.Ldstr, "---")
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null)
    myFCIL.Emit(OpCodes.Ldloc_S, v0)
    myFCIL.EmitCall(OpCodes.Call, getFilesMI, null)
    myFCIL.Emit(OpCodes.Stloc_S, v3)

    myFCIL.Emit(OpCodes.Br_S, evalForEachLabel)

    // foreach loop starts here.
    myFCIL.MarkLabel topOfForEachLabel
    
        // Load array of strings and index, store value at index for output.
    myFCIL.Emit(OpCodes.Ldloc_S, v3)
    myFCIL.Emit(OpCodes.Ldloc_S, v1)
    myFCIL.Emit OpCodes.Ldelem_Ref
    myFCIL.Emit(OpCodes.Stloc_S, v2)

    myFCIL.Emit(OpCodes.Ldloc_S, v2)
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null)

    // Increment counter by one.
    myFCIL.Emit(OpCodes.Ldloc_S, v1)
    myFCIL.Emit(OpCodes.Ldc_I4_1)
    myFCIL.Emit OpCodes.Add
    myFCIL.Emit(OpCodes.Stloc_S, v1)

    // Determine if end of file list array has been reached.
    myFCIL.MarkLabel evalForEachLabel
    myFCIL.Emit(OpCodes.Ldloc_S, v1)
    myFCIL.Emit(OpCodes.Ldloc_S, v3)
    myFCIL.Emit OpCodes.Ldlen
    myFCIL.Emit OpCodes.Conv_I4
    myFCIL.Emit(OpCodes.Blt_S, topOfForEachLabel)
    //foreach loop end here.

    myFCIL.Emit(OpCodes.Ldstr, "---")
    myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, null)
    myFCIL.Emit(OpCodes.Ldstr, "There are {0} files in {1}.")
    myFCIL.Emit(OpCodes.Ldloc_S, v1)
    myFCIL.Emit(OpCodes.Box, typeof<int>)
    myFCIL.Emit(OpCodes.Ldloc_S, v0)
    myFCIL.EmitCall(OpCodes.Call, writeLine2objMI, null)

    myFCIL.Emit OpCodes.Ret

    let myType = myTypeBuilder.CreateType()

    myAsmBuilder.SetEntryPoint myFCMethod
    myAsmBuilder.Save executableName
    printfn "-- Method generated, type completed, and assembly saved to disk."

    myType


printf "Enter a name for the file counting assembly: "
let executableNameNoExe = stdin.ReadLine()
let executableName = executableNameNoExe + ".exe"
printfn "---"

let domainDir = Environment.CurrentDirectory

let mutable curDomain = Thread.GetDomain()

// Create a new AppDomain, with the current directory as the base.

printfn $"Current Directory: {Environment.CurrentDirectory}"
let mySetupInfo = AppDomainSetup()
mySetupInfo.ApplicationBase <- domainDir
mySetupInfo.ApplicationName <- executableNameNoExe
mySetupInfo.LoaderOptimization <- LoaderOptimization.SingleDomain

let myDomain = 
    AppDomain.CreateDomain(executableNameNoExe, null, mySetupInfo)

printfn $"Creating a new AppDomain '{executableNameNoExe}'..."

printfn $"-- Base Directory = '{myDomain.BaseDirectory}'"
printfn $"-- Shadow Copy? = '{myDomain.ShadowCopyFiles}'"

printfn "---"
let myFCType = 
    createADynamicAssembly &curDomain executableNameNoExe

printfn $"Loading '{executableName}' from '{myDomain.BaseDirectory}'..."

let bFlags = 
    BindingFlags.Public ||| BindingFlags.CreateInstance ||| BindingFlags.Instance

let myObjInstance = 
    myDomain.CreateInstanceAndUnwrap(executableNameNoExe,
            executableNameNoExe, false, bFlags,
            null, null, null, null, null)

printfn $"Executing method 'CountLocalFiles' in {myObjInstance}..."

myFCType.InvokeMember("CountLocalFiles", BindingFlags.InvokeMethod, null, myObjInstance, [||])
Imports System.IO
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.Remoting



Class ADDyno
   
   
   
   Public Shared Function CreateADynamicAssembly(ByRef myNewDomain As AppDomain, executableNameNoExe As String) As Type
      
      Dim executableName As String = executableNameNoExe + ".exe"
      
      Dim myAsmName As New AssemblyName()
      myAsmName.Name = executableNameNoExe
      myAsmName.CodeBase = Environment.CurrentDirectory
      
      Dim myAsmBuilder As AssemblyBuilder = myNewDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave)
      Console.WriteLine("-- Dynamic Assembly instantiated.")
      
      Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule(executableNameNoExe, executableName)
      
      Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType(executableNameNoExe, TypeAttributes.Public, GetType(MarshalByRefObject))
      
      Dim myFCMethod As MethodBuilder = myTypeBuilder.DefineMethod("CountLocalFiles", MethodAttributes.Public Or MethodAttributes.Static, Nothing, New Type() {})
      
      Dim currentDirGetMI As MethodInfo = GetType(Environment).GetProperty("CurrentDirectory").GetGetMethod()
      Dim writeLine0objMI As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() {GetType(String)})
      Dim writeLine2objMI As MethodInfo = GetType(Console).GetMethod("WriteLine", New Type() {GetType(String), GetType(Object), GetType(Object)})
      Dim getFilesMI As MethodInfo = GetType(Directory).GetMethod("GetFiles", New Type() {GetType(String)})
      
      myFCMethod.InitLocals = True
      
      Dim myFCIL As ILGenerator = myFCMethod.GetILGenerator()
      
      Console.WriteLine("-- Generating MSIL method body...")
      Dim v0 As LocalBuilder = myFCIL.DeclareLocal(GetType(String))
      Dim v1 As LocalBuilder = myFCIL.DeclareLocal(GetType(Integer))
      Dim v2 As LocalBuilder = myFCIL.DeclareLocal(GetType(String))
      Dim v3 As LocalBuilder = myFCIL.DeclareLocal(GetType(String()))
      
      Dim evalForEachLabel As Label = myFCIL.DefineLabel()
      Dim topOfForEachLabel As Label = myFCIL.DefineLabel()
      
      ' Build the method body.
      myFCIL.EmitCall(OpCodes.Call, currentDirGetMI, Nothing)
      myFCIL.Emit(OpCodes.Stloc_S, v0)
      myFCIL.Emit(OpCodes.Ldc_I4_0)
      myFCIL.Emit(OpCodes.Stloc_S, v1)
      myFCIL.Emit(OpCodes.Ldstr, "---")
      myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
      myFCIL.Emit(OpCodes.Ldloc_S, v0)
      myFCIL.EmitCall(OpCodes.Call, getFilesMI, Nothing)
      myFCIL.Emit(OpCodes.Stloc_S, v3)
      
      myFCIL.Emit(OpCodes.Br_S, evalForEachLabel)
      
      ' foreach loop starts here.
      myFCIL.MarkLabel(topOfForEachLabel)
      
      ' Load array of strings and index, store value at index for output.
      myFCIL.Emit(OpCodes.Ldloc_S, v3)
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Ldelem_Ref)
      myFCIL.Emit(OpCodes.Stloc_S, v2)
      
      myFCIL.Emit(OpCodes.Ldloc_S, v2)
      myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
      
      ' Increment counter by one.
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Ldc_I4_1)
      myFCIL.Emit(OpCodes.Add)
      myFCIL.Emit(OpCodes.Stloc_S, v1)
      
      ' Determine if end of file list array has been reached.
      myFCIL.MarkLabel(evalForEachLabel)
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Ldloc_S, v3)
      myFCIL.Emit(OpCodes.Ldlen)
      myFCIL.Emit(OpCodes.Conv_I4)
      myFCIL.Emit(OpCodes.Blt_S, topOfForEachLabel)
      'foreach loop end here.
      myFCIL.Emit(OpCodes.Ldstr, "---")
      myFCIL.EmitCall(OpCodes.Call, writeLine0objMI, Nothing)
      myFCIL.Emit(OpCodes.Ldstr, "There are {0} files in {1}.")
      myFCIL.Emit(OpCodes.Ldloc_S, v1)
      myFCIL.Emit(OpCodes.Box, GetType(Integer))
      myFCIL.Emit(OpCodes.Ldloc_S, v0)
      myFCIL.EmitCall(OpCodes.Call, writeLine2objMI, Nothing)
      
      myFCIL.Emit(OpCodes.Ret)
      
      Dim myType As Type = myTypeBuilder.CreateType()
      
      myAsmBuilder.SetEntryPoint(myFCMethod)
      myAsmBuilder.Save(executableName)
      Console.WriteLine("-- Method generated, type completed, and assembly saved to disk.")
      
      Return myType
   End Function 'CreateADynamicAssembly
    
   
   Public Shared Sub Main()
      
      Dim executableName As String = Nothing
      Dim domainDir As String
      
      Console.Write("Enter a name for the file counting assembly: ")
      Dim executableNameNoExe As String = Console.ReadLine()
      executableName = executableNameNoExe + ".exe"
      Console.WriteLine("---")
      
      domainDir = Environment.CurrentDirectory
      
      Dim curDomain As AppDomain = Thread.GetDomain()
      
      
      ' Create a new AppDomain, with the current directory as the base.
      Console.WriteLine("Current Directory: {0}", Environment.CurrentDirectory)
      Dim mySetupInfo As New AppDomainSetup()
      mySetupInfo.ApplicationBase = domainDir
      mySetupInfo.ApplicationName = executableNameNoExe
      mySetupInfo.LoaderOptimization = LoaderOptimization.SingleDomain
      
      Dim myDomain As AppDomain = AppDomain.CreateDomain(executableNameNoExe, Nothing, mySetupInfo)
      
      Console.WriteLine("Creating a new AppDomain '{0}'...", executableNameNoExe)
      
      Console.WriteLine("-- Base Directory = '{0}'", myDomain.BaseDirectory)
      Console.WriteLine("-- Shadow Copy? = '{0}'", myDomain.ShadowCopyFiles)
      
      Console.WriteLine("---")
      Dim myFCType As Type = CreateADynamicAssembly(curDomain, executableNameNoExe)
      
      Console.WriteLine("Loading '{0}' from '{1}'...", executableName, myDomain.BaseDirectory.ToString())
      
      
      Dim bFlags As BindingFlags = BindingFlags.Public Or BindingFlags.CreateInstance Or BindingFlags.Instance
      
      Dim myObjInstance As [Object] = myDomain.CreateInstanceAndUnwrap(executableNameNoExe, executableNameNoExe, False, bFlags, Nothing, Nothing, Nothing, Nothing, Nothing)
      
      Console.WriteLine("Executing method 'CountLocalFiles' in {0}...", myObjInstance.ToString())
      
      myFCType.InvokeMember("CountLocalFiles", BindingFlags.InvokeMethod, Nothing, myObjInstance, New Object() {})
   End Sub
End Class

注解

这是一种将 CreateInstanceObjectHandle.Unwrap相结合的便利方法。 此方法调用 typeName的无参数构造函数。

有关 assemblyName格式,请参阅 AssemblyName。 有关 typeName格式,请参阅 Type.FullName 属性。

注意

如果对 CreateInstanceAndUnwrap返回的 T1 类型的对象的方法 M 进行早期绑定调用,并且该方法对程序集中类型 T2 对象的方法进行早期绑定调用,C 当前程序集或包含 T1的程序集,则程序集 C 将加载到当前应用程序域中。 即使对 T1.M() 的早期绑定调用是在 DynamicMethod的正文或其他动态生成的代码中进行的,也会发生此加载。 如果当前域是默认域,则在进程结束之前,无法卸载程序集 C。 如果当前域稍后尝试加载程序集 C,则加载可能会失败。

另请参阅

适用于

CreateInstanceAndUnwrap(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Source:
AppDomain.cs
Source:
AppDomain.cs
Source:
AppDomain.cs

创建指定程序集中定义的指定类型的新实例,指定是否忽略类型名称大小写;用于选择要创建的类型的绑定属性和绑定器;构造函数的参数;区域性;和激活属性。

public:
 System::Object ^ CreateInstanceAndUnwrap(System::String ^ assemblyName, System::String ^ typeName, bool ignoreCase, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes);
public object? CreateInstanceAndUnwrap (string assemblyName, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes);
public object CreateInstanceAndUnwrap (string assemblyName, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
member this.CreateInstanceAndUnwrap : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> obj
Public Function CreateInstanceAndUnwrap (assemblyName As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As Object

参数

assemblyName
String

程序集的显示名称。 请参阅 FullName

typeName
String

请求类型的完全限定名称,包括命名空间而不是程序集,由 FullName 属性返回。

ignoreCase
Boolean

一个布尔值,指定是否执行区分大小写的搜索。

bindingAttr
BindingFlags

影响搜索 typeName 构造函数的零个或多个位标志的组合。 如果 bindingAttr 为零,则会对公共构造函数执行区分大小写的搜索。

binder
Binder

一个对象,它允许绑定、强制参数类型、调用成员以及使用反射检索 MemberInfo 对象。 如果 binder 为 null,则使用默认绑定器。

args
Object[]

要传递给构造函数的参数。 此参数数组必须以数字、顺序和键入要调用的构造函数的参数匹配。 如果首选无参数构造函数,args 必须是空数组或 null。

culture
CultureInfo

一个特定于区域性的对象,用于控制类型的强制。 如果 culturenull,则使用当前线程的 CultureInfo

activationAttributes
Object[]

可以参与激活的一个或多个属性的数组。 通常,包含单个 UrlAttribute 对象的数组。 ,指定激活远程对象所需的 URL。

此参数与客户端激活的对象相关。 客户端激活是保留的旧技术,用于向后兼容,但不建议用于新开发。 分布式应用程序应改用 Windows Communication Foundation。

返回

typeName指定的对象的实例。

例外

assemblyNametypeNamenull

找不到匹配的构造函数。

assemblyName中找不到 typeName

找不到 assemblyName

调用方无权调用此构造函数。

调用方无法为不继承自 MarshalByRefObject的对象提供激活属性。

在卸载的应用程序域中尝试此操作。

assemblyName 当前加载的运行时不是有效的程序集。

程序集或模块加载了两次,其中包含两个不同的证据。

示例

下面的示例演示如何使用 ignoreCase 参数。

using namespace System;
using namespace System::Reflection;
static void InstantiateINT32( bool ignoreCase )
{
   try
   {
      AppDomain^ currentDomain = AppDomain::CurrentDomain;
      Object^ instance = currentDomain->CreateInstanceAndUnwrap( 
         "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", 
         "SYSTEM.INT32", 
         ignoreCase, 
         BindingFlags::Default, 
         nullptr, 
         nullptr, 
         nullptr, 
         nullptr, 
         nullptr );
      Console::WriteLine( instance->GetType() );
   }
   catch ( TypeLoadException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

int main()
{
   InstantiateINT32( false ); // Failed!
   InstantiateINT32( true ); // OK!
}
using System;
using System.Reflection;

class IgnoreCaseSnippet {

   static void Main() {
      InstantiateINT32(false);     // Failed!
      InstantiateINT32(true);      // OK!
   }

   static void InstantiateINT32(bool ignoreCase) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         object instance = currentDomain.CreateInstanceAndUnwrap(
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "SYSTEM.INT32",
            ignoreCase,
            BindingFlags.Default,
            null,
            null,
            null,
            null,
            null
         );
         Console.WriteLine(instance.GetType());
      } catch (TypeLoadException e) {
         Console.WriteLine(e.Message);
      }
   }
}
open System
open System.Reflection


let instantiateINT32 ignoreCase =
    try
        let currentDomain = AppDomain.CurrentDomain
        let instance = currentDomain.CreateInstanceAndUnwrap(
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "SYSTEM.INT32",
            ignoreCase,
            BindingFlags.Default,
            null,
            null,
            null,
            null,
            null)
        printfn $"{instance.GetType()}"
    with :? TypeLoadException as e ->
        printfn $"{e.Message}"

instantiateINT32 false     // Failed!
instantiateINT32 true      // OK!
Imports System.Reflection

Module Test

   Sub Main()
      InstantiateINT32(False)	' Failed!
      InstantiateINT32(True)	' OK!
   End Sub

   Sub InstantiateINT32(ignoreCase As Boolean)
      Try
         Dim currentDomain As AppDomain = AppDomain.CurrentDomain
         Dim instance As Object = currentDomain.CreateInstanceAndUnwrap( _
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", _
            "SYSTEM.INT32", _
            ignoreCase, _
            BindingFlags.Default, _
            Nothing, _
            Nothing, _
            Nothing, _
            Nothing, _
            Nothing  _
         )
         Console.WriteLine(instance.GetType())
      Catch e As TypeLoadException
         Console.WriteLine(e.Message)
      End Try
   End Sub

End Module 'Test

注解

这是一种将 CreateInstanceObjectHandle.Unwrap相结合的便利方法。

有关 assemblyName格式,请参阅 AssemblyName。 有关 typeName格式,请参阅 Type.FullName 属性。

注意

如果对 CreateInstanceAndUnwrap返回的 T1 类型的对象的方法 M 进行早期绑定调用,并且该方法对程序集中类型 T2 对象的方法进行早期绑定调用,C 当前程序集或包含 T1的程序集,则程序集 C 将加载到当前应用程序域中。 即使对 T1.M() 的早期绑定调用是在 DynamicMethod的正文或其他动态生成的代码中进行的,也会发生此加载。 如果当前域是默认域,则在进程结束之前,无法卸载程序集 C。 如果当前域稍后尝试加载程序集 C,则加载可能会失败。

另请参阅

适用于

CreateInstanceAndUnwrap(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)

注意

Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceAndUnwrap which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.

创建指定类型的新实例。 参数指定类型的名称及其创建方式。

public:
 System::Object ^ CreateInstanceAndUnwrap(System::String ^ assemblyName, System::String ^ typeName, bool ignoreCase, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes, System::Security::Policy::Evidence ^ securityAttributes);
public object CreateInstanceAndUnwrap (string assemblyName, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes, System.Security.Policy.Evidence securityAttributes);
[System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceAndUnwrap which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")]
public object CreateInstanceAndUnwrap (string assemblyName, string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes, System.Security.Policy.Evidence securityAttributes);
member this.CreateInstanceAndUnwrap : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> obj
[<System.Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceAndUnwrap which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")>]
member this.CreateInstanceAndUnwrap : string * string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] * System.Security.Policy.Evidence -> obj
Public Function CreateInstanceAndUnwrap (assemblyName As String, typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object(), securityAttributes As Evidence) As Object

参数

assemblyName
String

程序集的显示名称。 请参阅 FullName

typeName
String

请求类型的完全限定名称,包括命名空间而不是程序集,由 FullName 属性返回。

ignoreCase
Boolean

一个布尔值,指定是否执行区分大小写的搜索。

bindingAttr
BindingFlags

影响搜索 typeName 构造函数的零个或多个位标志的组合。 如果 bindingAttr 为零,则会对公共构造函数执行区分大小写的搜索。

binder
Binder

一个对象,它允许绑定、强制参数类型、调用成员以及使用反射检索 MemberInfo 对象。 如果 binder 为 null,则使用默认绑定器。

args
Object[]

要传递给构造函数的参数。 此参数数组必须以数字、顺序和键入要调用的构造函数的参数匹配。 如果首选无参数构造函数,args 必须是空数组或 null。

culture
CultureInfo

一个特定于区域性的对象,用于控制类型的强制。 如果 culturenull,则使用当前线程的 CultureInfo

activationAttributes
Object[]

可以参与激活的一个或多个属性的数组。 通常,包含单个 UrlAttribute 对象的数组,该对象指定激活远程对象所需的 URL。

此参数与客户端激活的对象相关。 客户端激活是保留的旧技术,用于向后兼容,但不建议用于新开发。 分布式应用程序应改用 Windows Communication Foundation。

securityAttributes
Evidence

用于授权创建 typeName的信息。

返回

typeName指定的对象的实例。

属性

例外

assemblyNametypeNamenull

找不到匹配的构造函数。

assemblyName中找不到 typeName

找不到 assemblyName

调用方无权调用此构造函数。

调用方无法为不继承自 MarshalByRefObject的对象提供激活属性。

在卸载的应用程序域中尝试此操作。

assemblyName 当前加载的运行时不是有效的程序集。

程序集或模块加载了两次,其中包含两个不同的证据。

示例

下面的示例演示如何使用 ignoreCase 参数。

using namespace System;
using namespace System::Reflection;
static void InstantiateINT32( bool ignoreCase )
{
   try
   {
      AppDomain^ currentDomain = AppDomain::CurrentDomain;
      Object^ instance = currentDomain->CreateInstanceAndUnwrap( 
         "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", 
         "SYSTEM.INT32", 
         ignoreCase, 
         BindingFlags::Default, 
         nullptr, 
         nullptr, 
         nullptr, 
         nullptr, 
         nullptr );
      Console::WriteLine( instance->GetType() );
   }
   catch ( TypeLoadException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

int main()
{
   InstantiateINT32( false ); // Failed!
   InstantiateINT32( true ); // OK!
}
using System;
using System.Reflection;

class IgnoreCaseSnippet {

   static void Main() {
      InstantiateINT32(false);     // Failed!
      InstantiateINT32(true);      // OK!
   }

   static void InstantiateINT32(bool ignoreCase) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         object instance = currentDomain.CreateInstanceAndUnwrap(
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "SYSTEM.INT32",
            ignoreCase,
            BindingFlags.Default,
            null,
            null,
            null,
            null,
            null
         );
         Console.WriteLine(instance.GetType());
      } catch (TypeLoadException e) {
         Console.WriteLine(e.Message);
      }
   }
}
open System
open System.Reflection


let instantiateINT32 ignoreCase =
    try
        let currentDomain = AppDomain.CurrentDomain
        let instance = currentDomain.CreateInstanceAndUnwrap(
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
            "SYSTEM.INT32",
            ignoreCase,
            BindingFlags.Default,
            null,
            null,
            null,
            null,
            null)
        printfn $"{instance.GetType()}"
    with :? TypeLoadException as e ->
        printfn $"{e.Message}"

instantiateINT32 false     // Failed!
instantiateINT32 true      // OK!
Imports System.Reflection

Module Test

   Sub Main()
      InstantiateINT32(False)	' Failed!
      InstantiateINT32(True)	' OK!
   End Sub

   Sub InstantiateINT32(ignoreCase As Boolean)
      Try
         Dim currentDomain As AppDomain = AppDomain.CurrentDomain
         Dim instance As Object = currentDomain.CreateInstanceAndUnwrap( _
            "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", _
            "SYSTEM.INT32", _
            ignoreCase, _
            BindingFlags.Default, _
            Nothing, _
            Nothing, _
            Nothing, _
            Nothing, _
            Nothing  _
         )
         Console.WriteLine(instance.GetType())
      Catch e As TypeLoadException
         Console.WriteLine(e.Message)
      End Try
   End Sub

End Module 'Test

注解

这是一种将 CreateInstanceObjectHandle.Unwrap相结合的便利方法。

有关 assemblyName格式,请参阅 AssemblyName。 有关 typeName格式,请参阅 Type.FullName 属性。

注意

如果对 CreateInstanceAndUnwrap返回的 T1 类型的对象的方法 M 进行早期绑定调用,并且该方法对程序集中类型 T2 对象的方法进行早期绑定调用,C 当前程序集或包含 T1的程序集,则程序集 C 将加载到当前应用程序域中。 即使对 T1.M() 的早期绑定调用是在 DynamicMethod的正文或其他动态生成的代码中进行的,也会发生此加载。 如果当前域是默认域,则在进程结束之前,无法卸载程序集 C。 如果当前域稍后尝试加载程序集 C,则加载可能会失败。

另请参阅

适用于