Activator 類別

定義

包含本機或遠端建立物件類型的方法,或者取得對現有遠端物件的參考。 此類別無法獲得繼承。

public ref class Activator abstract sealed
public ref class Activator sealed : System::Runtime::InteropServices::_Activator
public static class Activator
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class Activator : System.Runtime.InteropServices._Activator
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Activator : System.Runtime.InteropServices._Activator
type Activator = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type Activator = class
    interface _Activator
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Activator = class
    interface _Activator
Public Class Activator
Public NotInheritable Class Activator
Implements _Activator
繼承
Activator
屬性
實作

範例

下列範例示範如何使用 Activator 類別在執行時間動態建構物件。

using namespace System;
using namespace System::Reflection;
using namespace System::Text;

public ref class SomeType
{
public:
    void DoSomething(int x)
    {
        Console::WriteLine("100 / {0} = {1}", x, 100 / x);
    }
};

void main()
{
    // Create an instance of the StringBuilder type using 
    // Activator.CreateInstance.
    Object^ o = Activator::CreateInstance(StringBuilder::typeid);

    // Append a string into the StringBuilder object and display the 
    // StringBuilder.
    StringBuilder^ sb = (StringBuilder^) o;
    sb->Append("Hello, there.");
    Console::WriteLine(sb);

    // Create an instance of the SomeType class that is defined in this 
    // assembly.
    System::Runtime::Remoting::ObjectHandle^ oh = 
        Activator::CreateInstanceFrom(Assembly::GetEntryAssembly()->CodeBase, 
                                      SomeType::typeid->FullName);

    // Call an instance method defined by the SomeType type using this object.
    SomeType^ st = (SomeType^) oh->Unwrap();

    st->DoSomething(5);
};

/* This code produces the following output:
 
Hello, there.
100 / 5 = 20
 */
using System;
using System.Reflection;
using System.Text;

public class SomeType
{
    public void DoSomething(int x)
    {
        Console.WriteLine("100 / {0} = {1}", x, 100 / x);
    }
}

public class Example
{
    static void Main()
    {
        // Create an instance of the StringBuilder type using
        // Activator.CreateInstance.
        Object o = Activator.CreateInstance(typeof(StringBuilder));

        // Append a string into the StringBuilder object and display the
        // StringBuilder.
        StringBuilder sb = (StringBuilder) o;
        sb.Append("Hello, there.");
        Console.WriteLine(sb);

        // Create an instance of the SomeType class that is defined in this
        // assembly.
        System.Runtime.Remoting.ObjectHandle oh =
            Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase,
                                         typeof(SomeType).FullName);

        // Call an instance method defined by the SomeType type using this object.
        SomeType st = (SomeType) oh.Unwrap();

        st.DoSomething(5);
    }
}

/* This code produces the following output:

Hello, there.
100 / 5 = 20
 */
open System
open System.Reflection
open System.Text

type SomeType() =
    member _.DoSomething(x) = printfn $"100 / {x} = {100 / x}"

// Create an instance of the StringBuilder type using Activator.CreateInstance.
let o = Activator.CreateInstance typeof<StringBuilder>

// Append a string into the StringBuilder object and display the StringBuilder.
let sb = o :?> StringBuilder
sb.Append "Hello, there." |> ignore
printfn $"{sb}"

// Create an instance of the SomeType class that is defined in this assembly.
let oh = 
    Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().Location, typeof<SomeType>.FullName)

// Call an instance method defined by the SomeType type using this object.
let st = oh.Unwrap() :?> SomeType

st.DoSomething 5

(* This code produces the following output:

Hello, there.
100 / 5 = 20
 *)
Imports System.Reflection
Imports System.Text

Module Module1
    Sub Main()
        ' Create an instance of the StringBuilder type using 
        ' Activator.CreateInstance.
        Dim o As Object = Activator.CreateInstance(GetType(StringBuilder))

        ' Append a string into the StringBuilder object and display the 
        ' StringBuilder.
        Dim sb As StringBuilder = CType(o, StringBuilder)
        sb.Append("Hello, there.")
        Console.WriteLine(sb)

        ' Create an instance of the SomeType class that is defined in this assembly.
        Dim oh As System.Runtime.Remoting.ObjectHandle = _
            Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
                                         GetType(SomeType).FullName)

        ' Call an instance method defined by the SomeType type using this object.
        Dim st As SomeType = CType(oh.Unwrap(), SomeType)

        st.DoSomething(5)
    End Sub

    Class SomeType
        Public Sub DoSomething(ByVal x As Int32)
            Console.WriteLine("100 / {0} = {1}", x, 100 \ x)
        End Sub
    End Class
End Module

' This code produces the following output:
' 
' Hello, there.
' 100 / 5 = 20

備註

方法 CreateInstance 會叫用最符合指定引數的建構函式,以建立元件中定義的型別實例。 如果未指定任何引數,則會叫用不採用任何參數的建構函式,也就是無參數建構函式。

您必須有足夠的許可權來搜尋和呼叫建構函式;否則會擲回例外狀況。 根據預設,搜尋建構函式期間只會考慮公用建構函式。 如果找不到建構函式或無參數建構函式,則會擲回例外狀況。

binder 參數會指定物件,以搜尋元件中是否有適當的建構函式。 您可以指定自己的系結器和搜尋準則。 如果未指定系結器,則會使用預設系結器。 如需詳細資訊,請參閱 System.Reflection.BinderSystem.Reflection.BindingFlags 類別。

辨識項參數會影響建構函式的安全性原則和許可權。 如需詳細資訊,請參閱 System.Security.Policy.Evidence 類別。

類型的實例可以在本機或遠端月臺建立。 如果從遠端建立類型,則啟用屬性參數會指定遠端月臺的 URI。 建立實例的呼叫可能會在到達遠端月臺之前通過中繼網站。 其他啟用屬性可以修改呼叫在遠端和中繼月臺上運作的環境或內容。

如果在本機建立實例,則會傳回該物件的參考。 如果從遠端建立實例,則會傳回 Proxy 的參考。 遠端物件是透過 Proxy 操作,就像是本機物件一樣。

方法 GetObject 會建立目前執行中遠端物件的 Proxy、伺服器啟動的已知物件或 XML Web 服務。 您可以指定連線媒體,也就是通道。 如需詳細資訊,請參閱 System.Runtime.Remoting.Channels.ChannelServices 類別。

元件包含類型定義。 CreateInstance方法會從目前執行中的元件建立型別的實例。 方法 CreateInstanceFrom 會從包含元件的檔案建立 實例。 方法 CreateComInstanceFrom 會從包含元件的檔案建立 COM 物件的實例。

方法

CreateComInstanceFrom(String, String)

使用具名組件檔案和無參數建構函式,建立具有已指定名稱的 COM 物件執行個體。

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

使用具名組件檔案和無參數建構函式,建立具有已指定名稱的 COM 物件執行個體。

CreateInstance(ActivationContext)

建立類型的執行個體,這個類型是由指定的 ActivationContext 物件所指派。

CreateInstance(ActivationContext, String[])

建立類型的執行個體,這個類型是由指定的 ActivationContext 物件所指派,並且是以指定的自訂啟動資料所啟動。

CreateInstance(AppDomain, String, String)

使用具名組件和無參數建構函式,建立已在指定遠端網域中具有指定名稱的類型執行個體。

CreateInstance(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

使用最符合指定參數的具名組件和建構函式,建立已在指定遠端網域中具有指定名稱之類型的執行個體。

CreateInstance(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已淘汰.

使用最符合指定參數的具名組件和建構函式,建立已在指定遠端網域中具有指定名稱之類型的執行個體。

CreateInstance(String, String)

使用具名組件和無參數建構函式,建立具有已指定名稱的 COM 物件執行個體。

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

使用具名組件和最符合指定參數的建構函式,建立已具有指定名稱之類型的執行個體。

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已淘汰.

使用具名組件和最符合指定參數的建構函式,建立已具有指定名稱之類型的執行個體。

CreateInstance(String, String, Object[])

使用具名組件和無參數建構函式,建立具有已指定名稱的 COM 物件執行個體。

CreateInstance(Type)

使用所指定類型的無參數建構函式,建立該類型的執行個體。

CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo)

使用最符合指定之參數的建構函式,建立指定之類型的執行個體。

CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo, Object[])

使用最符合指定之參數的建構函式,建立指定之類型的執行個體。

CreateInstance(Type, Boolean)

使用所指定類型的無參數建構函式,建立該類型的執行個體。

CreateInstance(Type, Object[])

使用最符合指定之參數的建構函式,建立指定之類型的執行個體。

CreateInstance(Type, Object[], Object[])

使用最符合指定之參數的建構函式,建立指定之類型的執行個體。

CreateInstance<T>()

使用無參數建構函式,建立由指定之泛型類型參數所指定之類型的執行個體。

CreateInstanceFrom(AppDomain, String, String)

使用具名組件檔案和無參數建構函式,建立已在指定遠端網域中具有指定名稱之類型的執行個體。

CreateInstanceFrom(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

使用最符合指定參數的具名組件檔案和建構函式,建立已在指定遠端網域中指定名稱之類型的執行個體。

CreateInstanceFrom(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已淘汰.

使用最符合指定參數的具名組件檔案和建構函式,建立已在指定遠端網域中指定名稱之類型的執行個體。

CreateInstanceFrom(String, String)

使用具名組件檔案和無參數建構函式,建立已具有指定名稱的類型執行個體。

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

使用具名組件檔案和最符合指定參數的建構函式,建立已具有指定名稱之類型的執行個體。

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已淘汰.

使用具名組件檔案和最符合指定參數的建構函式,建立已具有指定名稱之類型的執行個體。

CreateInstanceFrom(String, String, Object[])

使用具名組件檔案和無參數建構函式,建立已具有指定名稱的類型執行個體。

GetObject(Type, String)

為指定類型和 URL 所指示的已知物件建立 Proxy。

GetObject(Type, String, Object)

為指定類型、URL 和通道資料所指示的已知物件建立 Proxy。

明確介面實作

_Activator.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

_Activator.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

_Activator.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

_Activator.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

適用於