Activator Activator Activator Activator Class

定义

包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用。Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects. 此类不能被继承。This class cannot be inherited.

public ref class Activator sealed : 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
    interface _Activator
Public NotInheritable Class Activator
Implements _Activator
继承
ActivatorActivatorActivatorActivator
属性
实现

示例

下面的示例演示如何使用Activator类在运行时动态构造对象。The following example shows how to use the Activator class to dynamically construct objects at run time.

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
 */
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方法通过调用与指定参数匹配程度最高的构造函数来创建在程序集中定义的类型的实例。The CreateInstance method creates an instance of a type defined in an assembly by invoking the constructor that best matches the specified arguments. 如果未指定任何参数,则调用不带任何参数的构造函数(即无参数的构造函数)。If no arguments are specified, the constructor that takes no parameters, that is, the parameterless constructor, is invoked.

您必须具有足够的权限才能搜索和调用构造函数;否则,将引发异常。You must have sufficient permission to search for and call a constructor; otherwise, an exception is thrown. 默认情况下,在搜索构造函数期间只考虑公共构造函数。By default, only public constructors are considered during the search for a constructor. 如果找不到任何构造函数或无参数构造函数,则会引发异常。If no constructor or parameterless constructor can be found, an exception is thrown.

联编程序参数指定一个对象,该对象在程序集上搜索合适的构造函数。A binder parameter specifies an object that searches an assembly for a suitable constructor. 您可以指定自己的联编程序和搜索条件。You can specify your own binder and search criteria. 如果未指定联编程序,则使用默认联编程序。If no binder is specified, a default binder is used. 有关更多信息,请参见 System.Reflection.BinderSystem.Reflection.BindingFlags 类。For more information, see the System.Reflection.Binder and System.Reflection.BindingFlags classes.

证据参数会影响构造函数的安全策略和权限。An evidence parameter affects the security policy and permissions for the constructor. 有关更多信息,请参见 System.Security.Policy.Evidence 类。For more information, see the System.Security.Policy.Evidence class.

可以在本地或远程站点创建类型的实例。An instance of a type can be created at a local or remote site. 如果类型是远程创建的,则激活特性参数将指定远程站点的 URI。If the type is created remotely, an activation attribute parameter specifies the URI of the remote site. 创建实例的调用可能会在到达远程站点之前通过中间站点。The call to create the instance might pass through intermediary sites before it reaches the remote site. 其他激活属性可以修改调用在远程和中间站点上的运行环境或上下文。Other activation attributes can modify the environment, or context, in which the call operates at the remote and intermediary sites.

如果实例是在本地创建的,则返回对该对象的引用。If the instance is created locally, a reference to that object is returned. 如果实例是远程创建的,则返回对代理的引用。If the instance is created remotely, a reference to a proxy is returned. 远程对象通过代理操作,就像它是本地对象一样。The remote object is manipulated through the proxy as if it were a local object.

GetObject方法为当前正在运行的远程对象、服务器激活的已知对象或 XML Web service 创建代理。The GetObject method creates a proxy to a currently running remote object, server-activated well-known object, or XML Web service. 可以指定连接介质,即通道。You can specify the connection medium, that is, the channel. 有关更多信息,请参见 System.Runtime.Remoting.Channels.ChannelServices 类。For more information, see the System.Runtime.Remoting.Channels.ChannelServices class.

程序集包含类型定义。Assemblies contain type definitions. CreateInstance方法从当前运行的程序集创建类型的实例。The CreateInstance method creates an instance of a type from a currently running assembly. CreateInstanceFrom方法从包含程序集的文件创建实例。The CreateInstanceFrom method creates an instance from a file that contains an assembly. CreateComInstanceFrom方法从包含程序集的文件创建 COM 对象的实例。The CreateComInstanceFrom method creates an instance of a COM object from a file that contains an assembly.

方法

CreateComInstanceFrom(String, String) CreateComInstanceFrom(String, String) CreateComInstanceFrom(String, String) CreateComInstanceFrom(String, String)

使用已命名的程序集文件和无参数构造函数,创建指定名称的 COM 对象的实例。Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

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

使用已命名的程序集文件和无参数构造函数,创建指定名称的 COM 对象的实例。Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

CreateInstance(ActivationContext) CreateInstance(ActivationContext) CreateInstance(ActivationContext) CreateInstance(ActivationContext)

创建类型的一个实例,该类型由指定的 ActivationContext 对象指定。Creates an instance of the type designated by the specified ActivationContext object.

CreateInstance(ActivationContext, String[]) CreateInstance(ActivationContext, String[]) CreateInstance(ActivationContext, String[]) CreateInstance(ActivationContext, String[])

创建类型的一个实例,该类型由指定的 ActivationContext 对象指定,并由指定的自定义激活数据激活。Creates an instance of the type that is designated by the specified ActivationContext object and activated with the specified custom activation data.

CreateInstance(AppDomain, String, String) CreateInstance(AppDomain, String, String) CreateInstance(AppDomain, String, String) CreateInstance(AppDomain, String, String)

使用命名的程序集和无参数构造函数,来创建其名称在指定的远程域中指定的类型的实例。Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and parameterless constructor.

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

使用命名的程序集和最匹配所指定参数的构造函数,来创建其名称在指定的远程域中指定的类型的实例。Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and the constructor that best matches the specified parameters.

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

使用命名的程序集和最匹配所指定参数的构造函数,来创建其名称在指定的远程域中指定的类型的实例。Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and the constructor that best matches the specified parameters.

CreateInstance(String, String) CreateInstance(String, String) CreateInstance(String, String) CreateInstance(String, String)

使用已命名的程序集和无参数构造函数,创建名称已指定的类型的实例。Creates an instance of the type whose name is specified, using the named assembly and parameterless constructor.

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

使用指定的程序集和与指定参数匹配程度最高的构造函数来创建指定名称的类型的实例。Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.

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

使用指定的程序集和与指定参数匹配程度最高的构造函数来创建指定名称的类型的实例。Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.

CreateInstance(String, String, Object[]) CreateInstance(String, String, Object[]) CreateInstance(String, String, Object[]) CreateInstance(String, String, Object[])

使用已命名的程序集和无参数构造函数,创建名称已指定的类型的实例。Creates an instance of the type whose name is specified, using the named assembly and parameterless constructor.

CreateInstance(Type) CreateInstance(Type) CreateInstance(Type) CreateInstance(Type)

使用类型的无参数构造函数创建指定类型的实例。Creates an instance of the specified type using that type's parameterless constructor.

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

使用与指定参数匹配程度最高的构造函数创建指定类型的实例。Creates an instance of the specified type using the constructor that best matches the specified parameters.

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

使用与指定参数匹配程度最高的构造函数创建指定类型的实例。Creates an instance of the specified type using the constructor that best matches the specified parameters.

CreateInstance(Type, Boolean) CreateInstance(Type, Boolean) CreateInstance(Type, Boolean) CreateInstance(Type, Boolean)

使用类型的无参数构造函数创建指定类型的实例。Creates an instance of the specified type using that type's parameterless constructor.

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

使用与指定参数匹配程度最高的构造函数创建指定类型的实例。Creates an instance of the specified type using the constructor that best matches the specified parameters.

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

使用与指定参数匹配程度最高的构造函数创建指定类型的实例。Creates an instance of the specified type using the constructor that best matches the specified parameters.

CreateInstance<T>() CreateInstance<T>() CreateInstance<T>() CreateInstance<T>()

使用无参数构造函数,创建指定泛型类型参数所指定类型的实例。Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.

CreateInstanceFrom(AppDomain, String, String) CreateInstanceFrom(AppDomain, String, String) CreateInstanceFrom(AppDomain, String, String) CreateInstanceFrom(AppDomain, String, String)

使用已命名的程序集文件和无参数构造函数,来创建其名称在指定的远程域中指定的类型的实例。Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly file and parameterless constructor.

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

使用命名的程序集文件和最匹配所指定参数的构造函数,来创建其名称在指定的远程域中指定的类型的实例。Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly file and the constructor that best matches the specified parameters.

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

使用命名的程序集文件和最匹配所指定参数的构造函数,来创建其名称在指定的远程域中指定的类型的实例。Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly file and the constructor that best matches the specified parameters.

CreateInstanceFrom(String, String) CreateInstanceFrom(String, String) CreateInstanceFrom(String, String) CreateInstanceFrom(String, String)

使用已命名的程序集文件和无参数构造函数,创建名称已指定的类型的实例。Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

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

使用指定的程序集文件和与指定参数匹配程度最高的构造函数来创建指定名称的类型的实例。Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

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

使用指定的程序集文件和与指定参数匹配程度最高的构造函数来创建指定名称的类型的实例。Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

CreateInstanceFrom(String, String, Object[]) CreateInstanceFrom(String, String, Object[]) CreateInstanceFrom(String, String, Object[]) CreateInstanceFrom(String, String, Object[])

使用已命名的程序集文件和无参数构造函数,创建名称已指定的类型的实例。Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObject(Type, String) GetObject(Type, String) GetObject(Type, String) GetObject(Type, String)

为指定类型和 URL 所指示的已知对象创建一个代理。Creates a proxy for the well-known object indicated by the specified type and URL.

GetObject(Type, String, Object) GetObject(Type, String, Object) GetObject(Type, String, Object) GetObject(Type, String, Object)

为指定类型、URL 和通道数据所指示的已知对象创建一个代理。Creates a proxy for the well-known object indicated by the specified type, URL, and channel data.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

_Activator.GetTypeInfo(UInt32, UInt32, IntPtr) _Activator.GetTypeInfo(UInt32, UInt32, IntPtr) _Activator.GetTypeInfo(UInt32, UInt32, IntPtr) _Activator.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can then be used to get the type information for an interface.

_Activator.GetTypeInfoCount(UInt32) _Activator.GetTypeInfoCount(UInt32) _Activator.GetTypeInfoCount(UInt32) _Activator.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

适用于