Activator 类

包含特定的方法,用以在本地或从远程创建对象类型,或获取对现有远程对象的引用。无法继承此类。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public NotInheritable Class Activator
    Implements _Activator
用法
Dim instance As Activator
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.None)] 
public sealed class Activator : _Activator
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::None)] 
public ref class Activator sealed : _Activator
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public final class Activator implements _Activator
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public final class Activator implements _Activator

备注

CreateInstance 方法通过调用与指定参数匹配程度最高的构造函数来创建在程序集中定义的类型的实例。如果没有指定任何参数,则将调用不带任何参数的构造函数(即默认构造函数)。

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

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

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

类型的实例可以在本地或远程站点上创建。如果从远程创建类型,一个激活属性参数将指定远程站点的 URI。在到达远程站点之前,创建实例的调用可能会通过中间站点进行传递。其他激活属性可以修改该调用在远程和中间站点上进行操作所处的环境(或上下文)。

如果在本地创建实例,将返回对该对象的引用。如果从远程创建实例,将返回对代理的引用。通过代理,远程对象可以像本地对象一样进行操作。

GetObject 方法创建当前运行的远程对象、由服务器激活的已知对象或 XML Web services 的代理。您可以指定连接介质(即通道)。有关更多信息,请参见 System.Runtime.Remoting.Channels.ChannelServices 类。

程序集包含类型定义。CreateInstance 方法从当前所运行的程序集创建类型的实例。CreateInstanceFrom 方法从包含程序集的文件创建实例。CreateComInstanceFrom 方法从包含程序集的文件创建 COM 对象的实例。

有关由服务器激活和由客户端激活的对象的更多信息,请参见 服务器激活 主题。

示例

下面的示例演示如何在运行时使用 Activator 类来动态构造对象。

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 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

继承层次结构

System.Object
  System.Activator

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Activator 成员
System 命名空间

其他资源

.NET Framework 远程处理概述