Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Activator Class

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class Activator

The Activator type exposes the following members.

  NameDescription
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360CreateInstance(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 360CreateInstance(Type, 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 360CreateInstance<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 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows 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 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top

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.

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


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

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.