AppDomain.CreateComInstanceFrom 方法

定义

创建指定 COM 类型的新实例。

重载

CreateComInstanceFrom(String, String)

创建指定 COM 类型的新实例。 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。

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

创建指定 COM 类型的新实例。 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。

CreateComInstanceFrom(String, String)

创建指定 COM 类型的新实例。 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。

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

文件的名称,该文件包含定义所请求的类型的程序集。

typeName
String

所请求类型的名称。

返回

ObjectHandle

一个对象,该对象是 typeName 指定的新实例的包装。 返回值需要打开包装才能访问真实对象。

例外

assemblyNametypeNamenull

无法加载此类型。

在卸载的应用程序域上尝试该操作。

未找到公共无形参构造函数。

assemblyName 未找到。

typeName 是一个抽象类。

  • 或 -

使用晚期绑定机制调用了此成员。

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

assemblyName 为空字符串 ("")。

assemblyName 不是有效的程序集。

一个程序集或模块用两个不同的证据加载了两次。

所引用的 COM 对象为 null

示例

以下示例演示

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);
      }
   }
}
open System
open System.Runtime.InteropServices

[<ComVisible true>]
type MyComVisibleType() =
    do
        printfn "MyComVisibleType instantiated!"

[<ComVisible false>]
type MyComNonVisibleType() =
    do
        printfn "MyComNonVisibleType instantiated!"

let createComInstance typeName =
    try
        let currentDomain = AppDomain.CurrentDomain
        let assemblyName = currentDomain.FriendlyName
        currentDomain.CreateComInstanceFrom(assemblyName, typeName)
        |> ignore
    with e ->
        printfn $"{e.Message}"

createComInstance "MyComNonVisibleType"   // Fail!
createComInstance "MyComVisibleType"      // OK!
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

注解

使用此方法远程创建对象,而无需在本地加载类型。

必须取消包装返回值才能访问实际对象。

System.Runtime.InteropServices.ComVisibleAttribute必须显式或默认情况下将具有true值的属性应用于此方法的 COM 类型,以创建该类型的实例;否则TypeLoadException将引发。

另请参阅

适用于

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

创建指定 COM 类型的新实例。 形参指定文件的名称,该文件包含含有类型和类型名称的程序集。

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

文件的名称,该文件包含定义所请求的类型的程序集。

typeName
String

所请求类型的名称。

hashValue
Byte[]

表示计算所得的哈希代码的值。

hashAlgorithm
AssemblyHashAlgorithm

表示程序集清单使用的哈希算法。

返回

ObjectHandle

一个对象,该对象是 typeName 指定的新实例的包装。 返回值需要打开包装才能访问真实对象。

例外

assemblyNametypeNamenull

无法加载此类型。

在卸载的应用程序域上尝试该操作。

未找到公共无形参构造函数。

assemblyFile 未找到。

typeName 是一个抽象类。

  • 或 -

使用晚期绑定机制调用了此成员。

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

assemblyFile 为空字符串 ("")。

assemblyFile 不是有效的程序集。

一个程序集或模块用两个不同的证据加载了两次。

所引用的 COM 对象为 null

注解

使用此方法远程创建对象,而无需在本地加载类型。

必须取消包装返回值才能访问实际对象。

System.Runtime.InteropServices.ComVisibleAttribute必须显式或默认情况下将具有true值的属性应用于此方法的 COM 类型,以创建该类型的实例;否则TypeLoadException将引发。

另请参阅

适用于