AppDomain.CreateComInstanceFrom 方法

定义

创建指定 COM 类型的新实例。Creates a new instance of a specified COM type.

重载

CreateComInstanceFrom(String, String)

创建指定 COM 类型的新实例。Creates a new instance of a specified COM type. 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。Parameters specify the name of a file that contains an assembly containing the type and the name of the type.

CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm)

创建指定 COM 类型的新实例。Creates a new instance of a specified COM type. 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。Parameters specify the name of a file that contains an assembly containing the type and the name of the type.

CreateComInstanceFrom(String, String)

创建指定 COM 类型的新实例。Creates a new instance of a specified COM type. 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。Parameters specify the name of a file that contains an assembly containing the type and the name of the type.

public:
 System::Runtime::Remoting::ObjectHandle ^ CreateComInstanceFrom(System::String ^ assemblyName, System::String ^ typeName);
public System.Runtime.Remoting.ObjectHandle CreateComInstanceFrom (string assemblyName, string typeName);
member this.CreateComInstanceFrom : string * string -> System.Runtime.Remoting.ObjectHandle
Public Function CreateComInstanceFrom (assemblyName As String, typeName As String) As ObjectHandle

参数

assemblyName
String

文件的名称,该文件包含定义所请求的类型的程序集。The name of a file containing an assembly that defines the requested type.

typeName
String

所请求类型的名称。The name of the requested type.

返回

ObjectHandle

一个对象,该对象是 typeName 指定的新实例的包装。An object that is a wrapper for the new instance specified by typeName. 返回值需要打开包装才能访问真实对象。The return value needs to be unwrapped to access the real object.

例外

assemblyNametypeNamenullassemblyName or typeName is null.

无法加载此类型。The type cannot be loaded.

在卸载的应用程序域上尝试该操作。The operation is attempted on an unloaded application domain.

未找到公共无形参构造函数。No public parameterless constructor was found.

assemblyName 未找到。assemblyName is not found.

typeName 是一个抽象类。typeName is an abstract class.

- 或 --or- 使用晚期绑定机制调用了此成员。This member was invoked with a late-binding mechanism.

调用方不能为非继承自 MarshalByRefObject 的对象提供激活属性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

assemblyName 为空字符串 ("")。assemblyName is an empty string ("").

assemblyName 不是有效的程序集。assemblyName is not a valid assembly.

一个程序集或模块用两个不同的证据加载了两次。An assembly or module was loaded twice with two different evidences.

所引用的 COM 对象为 nullThe COM object that is being referred to is null.

示例

下面的示例演示The following sample demonstrates

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

[ComVisible(true)]
public ref class MyComVisibleType
{
public:
   MyComVisibleType()
   {
      Console::WriteLine( "MyComVisibleType instantiated!" );
   }

};


[ComVisible(false)]
public ref class MyComNonVisibleType
{
public:
   MyComNonVisibleType()
   {
      Console::WriteLine( "MyComNonVisibleType instantiated!" );
   }

};

void CreateComInstance( String^ typeName )
{
   try
   {
      AppDomain^ currentDomain = AppDomain::CurrentDomain;
      String^ assemblyName = currentDomain->FriendlyName;
      currentDomain->CreateComInstanceFrom( assemblyName, typeName );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

int main()
{
   CreateComInstance( "MyComNonVisibleType" ); // Fail!
   CreateComInstance( "MyComVisibleType" ); // OK!
}

using System;
using System.Reflection;
using System.Runtime.InteropServices;

[ComVisible(true)]
class MyComVisibleType {
   public MyComVisibleType() {
      Console.WriteLine("MyComVisibleType instantiated!");
   }
}

[ComVisible(false)]
class MyComNonVisibleType {
   public MyComNonVisibleType() {
      Console.WriteLine("MyComNonVisibleType instantiated!");
   }
}

class Test {
   public static void Main() {
      CreateComInstance("MyComNonVisibleType");   // Fail!
      CreateComInstance("MyComVisibleType");      // OK!
   }

   static void CreateComInstance(string typeName) {
      try {
         AppDomain currentDomain = AppDomain.CurrentDomain;
         string assemblyName = currentDomain.FriendlyName;
         currentDomain.CreateComInstanceFrom(assemblyName, typeName);
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }
}
Imports System.Reflection
Imports System.Runtime.InteropServices

<ComVisible(True)> _
Class MyComVisibleType

   Public Sub New()
      Console.WriteLine("MyComVisibleType instantiated!")
   End Sub

End Class

<ComVisible(False)> _
Class MyComNonVisibleType

   Public Sub New()
      Console.WriteLine("MyComNonVisibleType instantiated!")
   End Sub

End Class

Module Test

   Sub Main()
      CreateComInstance("MyComNonVisibleType")   ' Fail!
      CreateComInstance("MyComVisibleType")      ' OK!
   End Sub

   Sub CreateComInstance(typeName As String)
      Try
         Dim currentDomain As AppDomain = AppDomain.CurrentDomain
         Dim assemblyName As String = currentDomain.FriendlyName
         currentDomain.CreateComInstanceFrom(assemblyName, typeName)
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub

End Module 'Test

注解

使用此方法可以远程创建对象,而无需在本地加载类型。Use this method to create objects remotely without having to load the type locally.

必须将返回值解包,才能访问真实对象。The return value must to be unwrapped to access the real object.

System.Runtime.InteropServices.ComVisibleAttribute true 必须显式或默认情况下将值为的属性应用于 COM 类型,此方法才能创建该类型的实例; 否则, TypeLoadException 将引发。A System.Runtime.InteropServices.ComVisibleAttribute attribute with a value of true must be applied either explicitly or by default to the COM type for this method to create an instance of that type; otherwise, TypeLoadException is thrown.

另请参阅

适用于

CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm)

创建指定 COM 类型的新实例。Creates a new instance of a specified COM type. 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。Parameters specify the name of a file that contains an assembly containing the type and the name of the type.

public:
 System::Runtime::Remoting::ObjectHandle ^ CreateComInstanceFrom(System::String ^ assemblyFile, System::String ^ typeName, cli::array <System::Byte> ^ hashValue, System::Configuration::Assemblies::AssemblyHashAlgorithm hashAlgorithm);
public System.Runtime.Remoting.ObjectHandle CreateComInstanceFrom (string assemblyFile, string typeName, byte[] hashValue, System.Configuration.Assemblies.AssemblyHashAlgorithm hashAlgorithm);
member this.CreateComInstanceFrom : string * string * byte[] * System.Configuration.Assemblies.AssemblyHashAlgorithm -> System.Runtime.Remoting.ObjectHandle
Public Function CreateComInstanceFrom (assemblyFile As String, typeName As String, hashValue As Byte(), hashAlgorithm As AssemblyHashAlgorithm) As ObjectHandle

参数

assemblyFile
String

文件的名称,该文件包含定义所请求的类型的程序集。The name of a file containing an assembly that defines the requested type.

typeName
String

所请求类型的名称。The name of the requested type.

hashValue
Byte[]

表示计算所得的哈希代码的值。Represents the value of the computed hash code.

hashAlgorithm
AssemblyHashAlgorithm

表示程序集清单使用的哈希算法。Represents the hash algorithm used by the assembly manifest.

返回

ObjectHandle

一个对象,该对象是 typeName 指定的新实例的包装。An object that is a wrapper for the new instance specified by typeName. 返回值需要打开包装才能访问真实对象。The return value needs to be unwrapped to access the real object.

例外

assemblyNametypeNamenullassemblyName or typeName is null.

无法加载此类型。The type cannot be loaded.

在卸载的应用程序域上尝试该操作。The operation is attempted on an unloaded application domain.

未找到公共无形参构造函数。No public parameterless constructor was found.

assemblyFile 未找到。assemblyFile is not found.

typeName 是一个抽象类。typeName is an abstract class.

- 或 --or- 使用晚期绑定机制调用了此成员。This member was invoked with a late-binding mechanism.

调用方不能为非继承自 MarshalByRefObject 的对象提供激活属性。The caller cannot provide activation attributes for an object that does not inherit from MarshalByRefObject.

assemblyFile 为空字符串 ("")。assemblyFile is the empty string ("").

assemblyFile 不是有效的程序集。assemblyFile is not a valid assembly.

一个程序集或模块用两个不同的证据加载了两次。An assembly or module was loaded twice with two different evidences.

所引用的 COM 对象为 nullThe COM object that is being referred to is null.

注解

使用此方法可以远程创建对象,而无需在本地加载类型。Use this method to create objects remotely without having to load the type locally.

必须将返回值解包,才能访问真实对象。The return value must to be unwrapped to access the real object.

System.Runtime.InteropServices.ComVisibleAttribute true 必须显式或默认情况下将值为的属性应用于 COM 类型,此方法才能创建该类型的实例; 否则, TypeLoadException 将引发。A System.Runtime.InteropServices.ComVisibleAttribute attribute with a value of true must be applied either explicitly or by default to the COM type for this method to create an instance of that type; otherwise, TypeLoadException is thrown.

另请参阅

适用于