Activator Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Contains methods to create types of objects locally. This class cannot be inherited.

Inheritance Hierarchy

System.Object
  System.Activator

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

Syntax

'Declaration
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class Activator
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class Activator

The Activator type exposes the following members.

Methods

  Name Description
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 CreateInstance(Type) Creates an instance of the specified type by using that type's default constructor.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 CreateInstance(Type, array<Object[]) Creates an instance of the specified type by using the constructor that best matches the specified parameters.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 CreateInstance<T>() Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

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.

The type to be created and the constructor to be invoked must be accessible. For example, if you are using AssemblyBuilder to create a dynamic assembly, you can invoke an internal constructor (Friend constructor in Visual Basic) for a type defined within the dynamic assembly. However, if you are using DynamicMethod to create a dynamic method, you cannot invoke internal constructors, because the dynamic method is hosted in an anonymous module in a system-provided assembly.

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.

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.

Examples

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

Imports System.Reflection
Imports System.Text

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim sbType As Type = GetType(StringBuilder)

      ' Create an instance of the StringBuilder type using Activator.CreateInstance
      ' and the parameterless constructor.
      Dim o As Object = Activator.CreateInstance(sbType)

      ' Append a string to the StringBuilder object and display the StringBuilder,
      ' late bound.
      sbType.InvokeMember("Append", _
         BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.InvokeMethod, _
         Type.DefaultBinder, _
         o, New Object() { "Hello, there." })
      outputBlock.Text &= o.ToString() & vbCrLf

      ' Create an instance of StringBuilder using the constructor that takes a 
      ' string.
      o = Activator.CreateInstance(sbType, New Object() { "Hello, there." })

      ' Append a string to the StringBuilder object and display the StringBuilder,
      ' late bound.
      sbType.InvokeMember("Append", _
         BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.InvokeMethod, _
         Type.DefaultBinder, _
         o, New Object() { " And hello again!" })
      outputBlock.Text &= o.ToString() & vbCrLf
   End Sub

End Class

' This code produces the following output:
' 
'Hello, there.
'Hello, there. And hello again!
using System;
using System.Reflection;
using System.Text;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Type sbType = typeof(StringBuilder);

      // Create an instance of the StringBuilder type using Activator.CreateInstance
      // and the parameterless constructor.
      object o = Activator.CreateInstance(sbType);

      // Append a string to the StringBuilder object and display the StringBuilder,
      // late bound.
      sbType.InvokeMember("Append", 
         BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, 
         Type.DefaultBinder, 
         o, new object[] {"Hello, there."});
      outputBlock.Text += o.ToString() + "\n";

      // Create an instance of StringBuilder using the constructor that takes a 
      // string.
      o = Activator.CreateInstance(sbType, new object[]{"Hello, there."});

      // Append a string to the StringBuilder object and display the StringBuilder,
      // late bound.
      sbType.InvokeMember("Append", 
         BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, 
         Type.DefaultBinder, 
         o, new object[] {" And hello again!"});
      outputBlock.Text += o.ToString() + "\n";
   }
}

/* This code produces the following output:

Hello, there.
Hello, there. And hello again!
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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

See Also

Reference

Other Resources