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 通过调用与指定参数最匹配的构造函数来创建程序集中定义的类型的实例。 如果未指定参数,则调用不采用任何参数的构造函数,即无参数构造函数。

必须具有足够的权限才能搜索和调用构造函数;否则,将引发异常。 默认情况下,在搜索构造函数期间只考虑公共构造函数。 如果找不到任何构造函数或无参数构造函数,则会引发异常。

绑定器参数指定一个对象,该对象在程序集中搜索合适的构造函数。 可以指定自己的活页夹和搜索条件。 如果未指定绑定器,则使用默认联编程序。 有关更多信息,请参见 System.Reflection.BinderSystem.Reflection.BindingFlags 类。

证据参数会影响构造函数的安全策略和权限。 有关更多信息,请参见 System.Security.Policy.Evidence 类。

可以在本地或远程站点创建类型的实例。 如果远程创建类型,则激活属性参数指定远程站点的 URI。 创建实例的调用可能会在到达远程站点之前通过中间站点。 其他激活属性可以修改在远程站点和中间站点执行调用的环境或上下文。

如果实例是在本地创建的,则返回对该对象的引用。 如果远程创建实例,则返回对代理的引用。 远程对象通过代理操作,就像它是本地对象一样。

方法 GetObject 创建当前正在运行的远程对象、服务器激活的已知对象或 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)

使用已命名的程序集和无参数构造函数,创建名称已指定的类型的实例。

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

使用指定的程序集和与指定参数匹配程度最高的构造函数来创建指定名称的类型的实例。

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
已过时.

使用指定的程序集和与指定参数匹配程度最高的构造函数来创建指定名称的类型的实例。

CreateInstance(String, String, Object[])

使用已命名的程序集和无参数构造函数,创建名称已指定的类型的实例。

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 所指示的已知对象创建一个代理。

GetObject(Type, String, Object)

为指定类型、URL 和通道数据所指示的已知对象创建一个代理。

显式接口实现

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

将一组名称映射为对应的一组调度标识符。

_Activator.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

_Activator.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

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

提供对某一对象公开的属性和方法的访问。

适用于