Activator 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
包含本機或遠端建立物件類型的方法,或者取得對現有遠端物件的參考。 此類別無法獲得繼承。
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.Binder 和 System.Reflection.BindingFlags 類別。
辨識項參數會影響建構函式的安全性原則和許可權。 如需詳細資訊,請參閱 System.Security.Policy.Evidence 類別。
類型的實例可以在本機或遠端月臺建立。 如果從遠端建立類型,則啟用屬性參數會指定遠端月臺的 URI。 建立實例的呼叫可能會在到達遠端月臺之前通過中繼網站。 其他啟用屬性可以修改呼叫在遠端和中繼月臺上運作的環境或內容。
如果在本機建立實例,則會傳回該物件的參考。 如果從遠端建立實例,則會傳回 Proxy 的參考。 遠端物件是透過 Proxy 操作,就像是本機物件一樣。
方法 GetObject 會建立目前執行中遠端物件的 Proxy、伺服器啟動的已知物件或 XML Web 服務。 您可以指定連線媒體,也就是通道。 如需詳細資訊,請參閱 System.Runtime.Remoting.Channels.ChannelServices 類別。
元件包含類型定義。 CreateInstance方法會從目前執行中的元件建立型別的實例。 方法 CreateInstanceFrom 會從包含元件的檔案建立 實例。 方法 CreateComInstanceFrom 會從包含元件的檔案建立 COM 物件的實例。
方法
明確介面實作
_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) |
提供物件所公開的屬性和方法的存取權。 |