Activator Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

Public NotInheritable Class Activator
public sealed class Activator

The Activator type exposes the following members.

Methods

Name Description
CreateInstance(Type) Creates an instance of the specified type by using that type's default constructor.
CreateInstance(Type, array<Object>[]()[]) Creates an instance of the specified type by using the constructor that best matches the specified parameters.
CreateInstance<(Of <(T>)>)()()() Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
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.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
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. 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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

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

System Namespace