Activator Class
Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
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 default 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 default 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. 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. For more information, see the System.Security.Policy.Evidence class.
An instance of a type can be created at a local or remote site. 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.
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. For more information, see the System.Runtime.Remoting.Channels.ChannelServices class.
Assemblies contain type definitions. The CreateInstance method creates an instance of a type from a currently running assembly. The CreateInstanceFrom method creates an instance from a file that contains an assembly. The CreateComInstanceFrom method creates an instance of a COM object from a file that contains an assembly.
For more information about server-activated and client-activated objects, see the Server Activation topic
The following example shows how to use the Activator class to dynamically construct objects at run time.
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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, 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, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.