System 命名空間


.NET Framework 類別庫
Activator 類別

更新:2007 年 11 月

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

命名空間:  System
組件:  mscorlib (在 mscorlib.dll 中)
語法

Visual Basic (宣告)
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class Activator _
    Implements _Activator
Visual Basic (使用方式)
Dim instance As Activator
C#
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class Activator : _Activator
Visual C++
[ClassInterfaceAttribute(ClassInterfaceType::None)]
[ComVisibleAttribute(true)]
public ref class Activator sealed : _Activator
J#
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */
/** @attribute ComVisibleAttribute(true) */
public final class Activator implements _Activator
JScript
public final class Activator implements _Activator
備註

CreateInstance 方法會叫用最符合指定引數的建構函式,建立在組件中定義的型別執行個體。如果未指定引數,則會叫用不接受參數的建構函式 (亦即預設的建構函式)。

您必須有足夠的使用權限可搜尋及呼叫建構函式,否則會擲回例外狀況。根據預設,在搜尋建構函式期間只會考慮公用 (Public) 建構函式。如果找不到建構函式或者沒有預設建構函式,則會擲回例外狀況。

繫結器 (Binder) 參數會指定在組件中搜尋適當建構函式的物件。您可以指定自己的繫結器 (Binder) 及搜尋準則。如果沒有指定繫結器,則會使用預設繫結器。如需詳細資訊,請參閱 System.Reflection..::.BinderSystem.Reflection..::.BindingFlags 類別。

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

型別的執行個體可以在本機或遠端站台建立。如果型別在遠端建立,則啟動屬性參數會指定遠端站台的 URI。建立執行個體的呼叫可能會在到達遠端站台前先經過居間站台。其他啟動屬性可以修改呼叫在遠端或居間站台運作的所在環境或內容。

如果是執行個體在本機建立,則會傳回對該物件的參考。如果是執行個體在遠端建立,則會傳回對 Proxy 的參考。遠端物件是經由 Proxy 加以管理,就好像是本機物件一樣。

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

組件包含型別定義。CreateInstance 方法會從目前執行中的組件建立型別的執行個體。CreateInstanceFrom 方法會從包含組件的檔案建立執行個體。CreateComInstanceFrom 方法會從包含組件的檔案建立 COM 物件的執行個體。

如需伺服器啟動物件和用戶端啟動物件的詳細資訊,請參閱伺服器啟動過程主題。

範例

下列範例說明如何使用 Activator 類別,在執行階段以動態方式建構物件。

Visual Basic
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
C#
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
 */
Visual C++
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
 */
繼承階層架構

System..::.Object
  System..::.Activator
執行緒安全

這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
平台

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求
版本資訊

.NET Framework

支援版本:3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

支援版本:3.5、2.0、1.0

XNA Framework

支援版本:2.0、1.0
請參閱

參考

其他資源

標記 :


Page view tracker