Activator Class
TOC
Collapse the table of content
Expand the table of content

Activator Class

 

Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects. This class cannot be inherited.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)


[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class Activator : _Activator

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticCreateComInstanceFrom(String, String)

Creates an instance of the COM object whose name is specified, using the named assembly file and the default constructor.

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

Creates an instance of the COM object whose name is specified, using the named assembly file and the default constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(ActivationContext)

Creates an instance of the type designated by the specified ActivationContext object.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(ActivationContext, String[])

Creates an instance of the type that is designated by the specified ActivationContext object and activated with the specified custom activation data.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(AppDomain, String, String)

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and default constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(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.

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

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

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(String, String)

Creates an instance of the type whose name is specified, using the named assembly and default constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(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.

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

Obsolete.Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(String, String, Object[])

Creates an instance of the type whose name is specified, using the named assembly and default constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(Type)

Creates an instance of the specified type using that type's default constructor.

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

Creates an instance of the specified type using the constructor that best matches the specified parameters.

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

Creates an instance of the specified type using the constructor that best matches the specified parameters.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(Type, Boolean)

Creates an instance of the specified type using that type's default constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(Type, Object[])

Creates an instance of the specified type using the constructor that best matches the specified parameters.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance(Type, Object[], Object[])

Creates an instance of the specified type using the constructor that best matches the specified parameters.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstance<T>()

Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstanceFrom(AppDomain, String, String)

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly file and default constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstanceFrom(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.

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

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

System_CAPS_pubmethodSystem_CAPS_staticCreateInstanceFrom(String, String)

Creates an instance of the type whose name is specified, using the named assembly file and default constructor.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstanceFrom(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.

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

Obsolete.Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

System_CAPS_pubmethodSystem_CAPS_staticCreateInstanceFrom(String, String, Object[])

Creates an instance of the type whose name is specified, using the named assembly file and default constructor.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticGetObject(Type, String)

Creates a proxy for the well-known object indicated by the specified type and URL.

System_CAPS_pubmethodSystem_CAPS_staticGetObject(Type, String, Object)

Creates a proxy for the well-known object indicated by the specified type, URL, and channel data.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethod_Activator.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

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

System_CAPS_pubinterfaceSystem_CAPS_privmethod_Activator.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

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

Provides access to properties and methods exposed by an object.

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.

The following example shows how to use the Activator class to dynamically construct objects at run time.

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

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft