語言

AppDomain.CreateComInstanceFrom 方法

定義

建立指定 COM 類型的新實例。

多載

名稱 Description
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例封裝的物件。 回傳值必須展開包裝才能存取實體物件。

例外狀況

assemblyName 或 typeName 為 null。

該類型無法裝填。

此操作嘗試在未載入的應用程式域上執行。

未發現公開的無參數建構器。

找不到 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例封裝的物件。 回傳值必須展開包裝才能存取實體物件。

例外狀況

assemblyFile 或 typeName 為 null。

該類型無法裝填。

此操作嘗試在未載入的應用程式域上執行。

未發現公開的無參數建構器。

找不到 assemblyFile。

typeName 是一個抽象類別。

-或-

這個成員是使用晚期綁定機制叫用的。

呼叫者無法為不繼承自 MarshalByRefObject的物件提供啟動屬性。

assemblyFile 是空字串(“”)。

assemblyFile 不是一個有效的組裝。

元件或模組已載入兩次,並具有兩個不同的辨識項。

所參考的 COM 物件為 null。

備註

使用此方法遠端建立物件,無需在本地載入型別。

必須先展開回傳值才能存取實體物件。

System.Runtime.InteropServices.ComVisibleAttribute必須將值為 的true屬性明確或預設套用到 COM 類型,才能建立該類型的實例;否則 TypeLoadException 會拋出。

另請參閱

適用於