913

One may not always know the Type of an object at compile-time, but may need to create an instance of the Type.

How do you get a new object instance from a Type?

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
tags2k
  • 82,117
  • 31
  • 79
  • 106
  • 4
    Just as an extra to anyone using the above answers that implement: ObjectType instance = (ObjectType)Activator.CreateInstance(objectType); Be careful - if your Constructor isn't "Public" then you will get the following error: > "System.MissingMethodException: 'No parameterless constructor defined > for this object." Your class can be Internal/Friend, or whatever you need but the constructor must be public. – Nathan Evans Aug 13 '20 at 11:43

11 Answers11

1049

The Activator class within the root System namespace is pretty powerful.

There are a lot of overloads for passing parameters to the constructor and such. Check out the documentation at:

http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx

or (new path)

https://learn.microsoft.com/en-us/dotnet/api/system.activator.createinstance

Here are some simple examples:

ObjectType instance = (ObjectType)Activator.CreateInstance(objectType);

ObjectType instance = (ObjectType)Activator.CreateInstance("MyAssembly","MyNamespace.ObjectType");
Jay
  • 186
  • 2
  • 18
Karl Seguin
  • 21,574
  • 5
  • 44
  • 49
187

The answer was already given:

ObjectType instance = (ObjectType)Activator.CreateInstance(objectType);

However, the Activator class has a generic variant for the parameterless constructor that makes this slightly more readable by making the cast unnecessary and not needing to pass the runtime type of the object:

ObjectType instance = Activator.CreateInstance<ObjectType>();
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 11
    @Kevin Of course. Such an operation *can’t* work in a statically typed language because it doesn’t make sense. You cannot invoke methods on an object of unknown type. In the meantime (= since writing this answer) C# has got the `dynamic` construct which *does* allow such constructs but for most purposes this answer still covers it. – Konrad Rudolph Apr 07 '12 at 15:47
  • 2
    @KonradRudolph Not quite true. First of c# *does* allow you to create new types at runtime. You just can't call anything on them *in a statically safe way*. So yeah, you are half correct. But more realistically you need this when you load assemblies at runtime, which means the type isn't known at compile time. C# would be severely limited if you couldn't do this. I mean you just proved it yourself: how else does the Activator method that takes a type-instance work? When MS wrote the Activator class they had no compile-time knowledge of any future types users would write. – AnorZaken May 31 '17 at 17:27
  • 1
    @AnorZaken My comment says nothing about creating types at runtime. Of course you can do that, but you can’t use them *statically* in the same context (you can host a full statically compiled program of course). That’s all my comment is saying. – Konrad Rudolph May 31 '17 at 17:31
  • @KonradRudolph Ah sorry, misinterpreted "such an operation" to mean instantiating a type that's only known at runtime; instead of meaning using a runtime type as a generic type parameter. – AnorZaken May 31 '17 at 18:01
  • 1
    @AnorZaken -- technically you can both create new types at runtime AND call methods on them in a statically safe way *if* your new type implements a known interface or inherits a known base class. -- Either of those approaches will give you a static contract for your runtime created object. – BrainSlugs83 Oct 16 '18 at 17:17
  • 1
    What is a realistic use-case for `Activator.CreateInstance`? If you know `T` at design-time, wouldn't you just do `new T(...)`? – Andreas Warberg Sep 23 '21 at 12:12
  • @AndreasWarberg `T` would be an interface or an (abstract) base class in the case suggested here. You can’t create those. This is fundamentally *the only way* you can use dynamically instantiated types in a statically typed language (though C# now has optional dynamic typing, so it does allow other ways). – Konrad Rudolph Sep 23 '21 at 12:33
180

Compiled expression is best way! (for performance to repeatedly create instance in runtime).

static readonly Func<X> YCreator = Expression.Lambda<Func<X>>(
   Expression.New(typeof(Y).GetConstructor(Type.EmptyTypes))
 ).Compile();

X x = YCreator();

Statistics (2012):

    Iterations: 5000000
    00:00:00.8481762, Activator.CreateInstance(string, string)
    00:00:00.8416930, Activator.CreateInstance(type)
    00:00:06.6236752, ConstructorInfo.Invoke
    00:00:00.1776255, Compiled expression
    00:00:00.0462197, new

Statistics (2015, .net 4.5, x64):

    Iterations: 5000000
    00:00:00.2659981, Activator.CreateInstance(string, string)
    00:00:00.2603770, Activator.CreateInstance(type)
    00:00:00.7478936, ConstructorInfo.Invoke
    00:00:00.0700757, Compiled expression
    00:00:00.0286710, new

Statistics (2015, .net 4.5, x86):

    Iterations: 5000000
    00:00:00.3541501, Activator.CreateInstance(string, string)
    00:00:00.3686861, Activator.CreateInstance(type)
    00:00:00.9492354, ConstructorInfo.Invoke
    00:00:00.0719072, Compiled expression
    00:00:00.0229387, new

Statistics (2017, LINQPad 5.22.02/x64/.NET 4.6):

    Iterations: 5000000
    No args
    00:00:00.3897563, Activator.CreateInstance(string assemblyName, string typeName)
    00:00:00.3500748, Activator.CreateInstance(Type type)
    00:00:01.0100714, ConstructorInfo.Invoke
    00:00:00.1375767, Compiled expression
    00:00:00.1337920, Compiled expression (type)
    00:00:00.0593664, new
    Single arg
    00:00:03.9300630, Activator.CreateInstance(Type type)
    00:00:01.3881770, ConstructorInfo.Invoke
    00:00:00.1425534, Compiled expression
    00:00:00.0717409, new

Statistics (2019, x64/.NET 4.8):

Iterations: 5000000
No args
00:00:00.3287835, Activator.CreateInstance(string assemblyName, string typeName)
00:00:00.3122015, Activator.CreateInstance(Type type)
00:00:00.8035712, ConstructorInfo.Invoke
00:00:00.0692854, Compiled expression
00:00:00.0662223, Compiled expression (type)
00:00:00.0337862, new
Single arg
00:00:03.8081959, Activator.CreateInstance(Type type)
00:00:01.2507642, ConstructorInfo.Invoke
00:00:00.0671756, Compiled expression
00:00:00.0301489, new

Statistics (2019, x64/.NET Core 3.0):

Iterations: 5000000
No args
00:00:00.3226895, Activator.CreateInstance(string assemblyName, string typeName)
00:00:00.2786803, Activator.CreateInstance(Type type)
00:00:00.6183554, ConstructorInfo.Invoke
00:00:00.0483217, Compiled expression
00:00:00.0485119, Compiled expression (type)
00:00:00.0434534, new
Single arg
00:00:03.4389401, Activator.CreateInstance(Type type)
00:00:01.0803609, ConstructorInfo.Invoke
00:00:00.0554756, Compiled expression
00:00:00.0462232, new

Full code:

static X CreateY_New()
{
    return new Y();
}

static X CreateY_New_Arg(int z)
{
    return new Y(z);
}

static X CreateY_CreateInstance()
{
    return (X)Activator.CreateInstance(typeof(Y));
}

static X CreateY_CreateInstance_String()
{
    return (X)Activator.CreateInstance("Program", "Y").Unwrap();
}

static X CreateY_CreateInstance_Arg(int z)
{
    return (X)Activator.CreateInstance(typeof(Y), new object[] { z, });
}

private static readonly System.Reflection.ConstructorInfo YConstructor =
    typeof(Y).GetConstructor(Type.EmptyTypes);
private static readonly object[] Empty = new object[] { };
static X CreateY_Invoke()
{
    return (X)YConstructor.Invoke(Empty);
}

private static readonly System.Reflection.ConstructorInfo YConstructor_Arg =
    typeof(Y).GetConstructor(new[] { typeof(int), });
static X CreateY_Invoke_Arg(int z)
{
    return (X)YConstructor_Arg.Invoke(new object[] { z, });
}

private static readonly Func<X> YCreator = Expression.Lambda<Func<X>>(
   Expression.New(typeof(Y).GetConstructor(Type.EmptyTypes))
).Compile();
static X CreateY_CompiledExpression()
{
    return YCreator();
}

private static readonly Func<X> YCreator_Type = Expression.Lambda<Func<X>>(
   Expression.New(typeof(Y))
).Compile();
static X CreateY_CompiledExpression_Type()
{
    return YCreator_Type();
}

private static readonly ParameterExpression YCreator_Arg_Param = Expression.Parameter(typeof(int), "z");
private static readonly Func<int, X> YCreator_Arg = Expression.Lambda<Func<int, X>>(
   Expression.New(typeof(Y).GetConstructor(new[] { typeof(int), }), new[] { YCreator_Arg_Param, }),
   YCreator_Arg_Param
).Compile();
static X CreateY_CompiledExpression_Arg(int z)
{
    return YCreator_Arg(z);
}

static void Main(string[] args)
{
    const int iterations = 5000000;

    Console.WriteLine("Iterations: {0}", iterations);

    Console.WriteLine("No args");
    foreach (var creatorInfo in new[]
    {
        new {Name = "Activator.CreateInstance(string assemblyName, string typeName)", Creator = (Func<X>)CreateY_CreateInstance},
        new {Name = "Activator.CreateInstance(Type type)", Creator = (Func<X>)CreateY_CreateInstance},
        new {Name = "ConstructorInfo.Invoke", Creator = (Func<X>)CreateY_Invoke},
        new {Name = "Compiled expression", Creator = (Func<X>)CreateY_CompiledExpression},
        new {Name = "Compiled expression (type)", Creator = (Func<X>)CreateY_CompiledExpression_Type},
        new {Name = "new", Creator = (Func<X>)CreateY_New},
    })
    {
        var creator = creatorInfo.Creator;

        var sum = 0;
        for (var i = 0; i < 1000; i++)
            sum += creator().Z;

        var stopwatch = new Stopwatch();
        stopwatch.Start();
        for (var i = 0; i < iterations; ++i)
        {
            var x = creator();
            sum += x.Z;
        }
        stopwatch.Stop();
        Console.WriteLine("{0}, {1}", stopwatch.Elapsed, creatorInfo.Name);
    }

    Console.WriteLine("Single arg");
    foreach (var creatorInfo in new[]
    {
        new {Name = "Activator.CreateInstance(Type type)", Creator = (Func<int, X>)CreateY_CreateInstance_Arg},
        new {Name = "ConstructorInfo.Invoke", Creator = (Func<int, X>)CreateY_Invoke_Arg},
        new {Name = "Compiled expression", Creator = (Func<int, X>)CreateY_CompiledExpression_Arg},
        new {Name = "new", Creator = (Func<int, X>)CreateY_New_Arg},
    })
    {
        var creator = creatorInfo.Creator;

        var sum = 0;
        for (var i = 0; i < 1000; i++)
            sum += creator(i).Z;

        var stopwatch = new Stopwatch();
        stopwatch.Start();
        for (var i = 0; i < iterations; ++i)
        {
            var x = creator(i);
            sum += x.Z;
        }
        stopwatch.Stop();
        Console.WriteLine("{0}, {1}", stopwatch.Elapsed, creatorInfo.Name);
    }
}

public class X
{
  public X() { }
  public X(int z) { this.Z = z; }
  public int Z;
}

public class Y : X
{
    public Y() {}
    public Y(int z) : base(z) {}
}
PeterE
  • 5,715
  • 5
  • 29
  • 51
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61
30

Its pretty simple. Assume that your classname is Car and the namespace is Vehicles, then pass the parameter as Vehicles.Car which returns object of type Car. Like this you can create any instance of any class dynamically.

public object GetInstance(string strNamesapace)
{         
     Type t = Type.GetType(strNamesapace); 
     return  Activator.CreateInstance(t);         
}

If your Fully Qualified Name(ie, Vehicles.Car in this case) is in another assembly, the Type.GetType will be null. In such cases, you have loop through all assemblies and find the Type. For that you can use the below code

public object GetInstance(string strFullyQualifiedName)
{
     Type type = Type.GetType(strFullyQualifiedName);
     if (type != null)
         return Activator.CreateInstance(type);
     foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
     {
         type = asm.GetType(strFullyQualifiedName);
         if (type != null)
             return Activator.CreateInstance(type);
     }
     return null;
 }

And you can get the instance by calling the above method.

object objClassInstance = GetInstance("Vehicles.Car");
Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86
  • In your second case (external assembly), you could just pass in "Vehicles.Car,OtherAssembly" to your first method and it will work. Obviously OtherAssembly is the name of the assembly it lives in. – danmiser Oct 20 '16 at 21:24
  • 3
    @danmiser That needs hard coding the assembly name. In order to implement flexibility I am checking null and the code works in dynamic way :) – Sarath Subramanian Oct 21 '16 at 05:50
24

Without use of Reflection:

private T Create<T>() where T : class, new()
{
    return new T();
}
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 18
    How is this useful? You have to know the type already to call that method, and if you know the type you can construct it without a special method. – Kyle Delaney Feb 13 '17 at 02:21
  • So T can vary at runtime. Useful if you work with deríved Types. –  Feb 13 '17 at 09:41
  • a new T(); would fail if T isn't a reference type with parameterless constructor, This methods uses contraints to ensure T is reference type and has a constructor. –  Feb 13 '17 at 09:46
  • 5
    How can T vary at runtime? Don't you have to know T at design time in order to call Create <>? – Kyle Delaney Feb 15 '17 at 21:00
  • If you work with generic classes and interfaces in factories, the types that implements the interface should be instanciated may vary. –  Feb 16 '17 at 07:48
  • You know all possible types at compile time, but you don't know wich implementation will used at runtime. –  Feb 16 '17 at 07:54
  • @RobertP. You can create new types at runtime. There is no rule that says you know all types at compile time. And no I don't mean with generics. You can create completely new types, and add all fields, properties, and methods etc. at runtime. There is also the simple case where you want to create an instance of a type that's in an assembly that is not known at compile time. That is rather common. – AnorZaken May 31 '17 at 00:47
17

If this is for something that will be called a lot in an application instance, it's a lot faster to compile and cache dynamic code instead of using the activator or ConstructorInfo.Invoke(). Two easy options for dynamic compilation are compiled Linq Expressions or some simple IL opcodes and DynamicMethod. Either way, the difference is huge when you start getting into tight loops or multiple calls.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Tom Mayfield
  • 6,235
  • 2
  • 32
  • 43
14

If you want to use the default constructor then the solution using System.Activator presented earlier is probably the most convenient. However, if the type lacks a default constructor or you have to use a non-default one, then an option is to use reflection or System.ComponentModel.TypeDescriptor. In case of reflection, it is enough to know just the type name (with its namespace).

Example using reflection:

ObjectType instance = 
    (ObjectType)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(
        typeName: objectType.FulName, // string including namespace of the type
        ignoreCase: false,
        bindingAttr: BindingFlags.Default,
        binder: null,  // use default binder
        args: new object[] { args, to, constructor },
        culture: null, // use CultureInfo from current thread
        activationAttributes: null
    );

Example using TypeDescriptor:

ObjectType instance = 
    (ObjectType)System.ComponentModel.TypeDescriptor.CreateInstance(
        provider: null, // use standard type description provider, which uses reflection
        objectType: objectType,
        argTypes: new Type[] { types, of, args },
        args: new object[] { args, to, constructor }
    );
BSharp
  • 917
  • 8
  • 12
12

Given this problem the Activator will work when there is a parameterless ctor. If this is a constraint consider using

System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject()
Thulani Chivandikwa
  • 3,402
  • 30
  • 33
10

Wouldn't the generic T t = new T(); work?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
7
public AbstractType New
{
    get
    {
        return (AbstractType) Activator.CreateInstance(GetType());
    }
}
slavoo
  • 5,798
  • 64
  • 37
  • 39
vikram nayak
  • 591
  • 5
  • 14
4

I can across this question because I was looking to implement a simple CloneObject method for arbitrary class (with a default constructor)

With generic method you can require that the type implements New().

Public Function CloneObject(Of T As New)(ByVal src As T) As T
    Dim result As T = Nothing
    Dim cloneable = TryCast(src, ICloneable)
    If cloneable IsNot Nothing Then
        result = cloneable.Clone()
    Else
        result = New T
        CopySimpleProperties(src, result, Nothing, "clone")
    End If
    Return result
End Function

With non-generic assume the type has a default constructor and catch an exception if it doesn't.

Public Function CloneObject(ByVal src As Object) As Object
    Dim result As Object = Nothing
    Dim cloneable As ICloneable
    Try
        cloneable = TryCast(src, ICloneable)
        If cloneable IsNot Nothing Then
            result = cloneable.Clone()
        Else
            result = Activator.CreateInstance(src.GetType())
            CopySimpleProperties(src, result, Nothing, "clone")
        End If
    Catch ex As Exception
        Trace.WriteLine("!!! CloneObject(): " & ex.Message)
    End Try
    Return result
End Function
Darrel Lee
  • 2,372
  • 22
  • 22