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

请求的类型的名称。

返回

一个对象,它是由 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 CreateComInstanceFromSnippet {
   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

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

返回

一个对象,它是由 typeName新实例指定的包装器。 需要取消包装返回值才能访问实际对象。

例外

assemblyFiletypeNamenull.

无法加载类型。

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

找不到公共无参数构造函数。

找不到 assemblyFile

typeName 是一个抽象类。

-或-

此成员是使用后期绑定机制调用的。

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

assemblyFile 是空字符串(“)。

assemblyFile 不是有效的程序集。

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

要引用的 COM 对象为 null.

注解

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

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

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

另请参阅

适用于