EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

Activator Class

May 11, 2014

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

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

public sealed class Activator

The Activator type exposes the following members.

  NameDescription
Public methodStatic memberCreateInstance(Type)Creates an instance of the specified type by using that type's default constructor.
Public methodStatic memberCreateInstance(Type, Object[])Creates an instance of the specified type by using the constructor that best matches the specified parameters.
Public methodStatic memberCreateInstance<T>()Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns 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. 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!
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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

Afficher:
© 2014 Microsoft