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 會擲回。

另請參閱

適用於