Create Object from Type - CSharp System

CSharp examples for System:Object

Description

Create Object from Type

Demo Code


using System;//from  w  w w. j  a  v  a 2s  .  c  om

public class Main{
        public static object CreateObject(Type type)
        {
            object obj2;
            try
            {
                if (type.ToString() == "System.String")
                {
                    return string.Empty;
                }
                obj2 = Activator.CreateInstance(type);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return obj2;
        }
        public static object CreateObject<T>()
        {
            return CreateObject(typeof(T));
        }
}

Related Tutorials