Posted by: Cirilo Meggiolaro | 03/20/2009

Tip of the day #157 – Creating types on the fly with Activator class

The System.Activator sealed class contains several methods to create instances of objects in runtime either locally or remotely and return existing instances of them. Let’s check the methods that are available under the class and an example on how to create an instance of a class of type Customer in runtime.

Methods

All methods available under the Activator class are static:

  • ObjectHandle CreateComInstanceFrom(string assemblyName, string typeName);
  • ObjectHandle CreateComInstanceFrom(string assemblyName, string typeName, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm);
  • T CreateInstance<T>();
  • ObjectHandle CreateInstance(ActivationContext activationContext);
  • object CreateInstance(Type type);
  • ObjectHandle CreateInstance(ActivationContext activationContext, string[] activationCustomData);
  • ObjectHandle CreateInstance(string assemblyName, string typeName);
  • object CreateInstance(Type type, bool nonPublic);
  • object CreateInstance(Type type, params object[] args);
  • ObjectHandle CreateInstance(AppDomain domain, string assemblyName, string typeName);
  • ObjectHandle CreateInstance(string assemblyName, string typeName, object[] activationAttributes);
  • object CreateInstance(Type type, object[] args, object[] activationAttributes);
  • object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture);
  • object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes);
  • ObjectHandle CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityInfo);
  • ObjectHandle CreateInstance(AppDomain domain, string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityAttributes);
  • ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName);
  • ObjectHandle CreateInstanceFrom(AppDomain domain, string assemblyFile, string typeName);
  • ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName, object[] activationAttributes);
  • ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityInfo);
  • ObjectHandle CreateInstanceFrom(AppDomain domain, string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityAttributes);
  • object GetObject(Type type, string url);
  • object GetObject(Type type, string url, object state);

You may have noticed that there are overloads available for a lot of different scenarios and purposes.

Example

The following example demonstrates how to create the instance on the fly. To start let’s assume our application has a type called Customer as described below:

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DOB { get; set; }

    public Customer(int id, string name, DateTime dob)
    {
        this.ID = id;
        this.Name = name;
        this.DOB = dob;
    }
}

Let’s use one of the Activator.CreateInstance overloads to create an instance of Customer type.

public static T GetInstance<T>(params object[] parms)
{
    return (T)Activator.CreateInstance(typeof(T), parms);
}

For this example, I created a generic method that accepts an array of objects as parameter and retrieves an instance of T type that for this case will be Customer type.

The array elements must match the quantity and sequence existing on the Customer class constructor. If a class constructor does not accept parameters so, you must pass null as parameter.

In the case of wrong parameters or an inexistent constructor overload that matches the parameters passed to the method a MissingMethodException will be thrown.

Create the instance now is pretty straightforward as shown on the following code snippet:

Customer cust = GetInstance<Customer>(1, “Some guy”, new DateTime(1972, 5, 20));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: