Instantiating Types

There are two ways to dynamically instantiate an object from its type:

Activator.CreateInstance accepts a Type and optional arguments that get passed to the constructor:


using System;
using System.Reflection;
using System.Collections.Generic;

class MainClass
{
    static void Main()
    {
        int i = (int)Activator.CreateInstance(typeof(int));

        DateTime dt = (DateTime)Activator.CreateInstance(typeof(DateTime),2000, 1, 1);

    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.