112

Take the following class as an example:

class Sometype
{
    int someValue;

    public Sometype(int someValue)
    {
        this.someValue = someValue;
    }
}

I then want to create an instance of this type using reflection:

Type t = typeof(Sometype);
object o = Activator.CreateInstance(t);

Normally this will work, however because SomeType has not defined a parameterless constructor, the call to Activator.CreateInstance will throw an exception of type MissingMethodException with the message "No parameterless constructor defined for this object." Is there an alternative way to still create an instance of this type? It'd be kinda sucky to add parameterless constructors to all my classes.

2
  • 4
    FormatterServices.GetUninitializedObject not allow to create uninitialized string. You may get exception: System.ArgumentException: Uninitialized Strings cannot be created. Please keep this in mind. Commented Jan 15, 2010 at 8:14
  • Thanks for the heads up, but I'm already handling strings and basic types separately. Commented Jan 15, 2010 at 15:14

4 Answers 4

161

I originally posted this answer here, but here is a reprint since this isn't the exact same question but has the same answer:

FormatterServices.GetUninitializedObject() will create an instance without calling a constructor. I found this class by using Reflector and digging through some of the core .Net serialization classes.

I tested it using the sample code below and it looks like it works great:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;

namespace NoConstructorThingy
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = (MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass)); //does not call ctor
            myClass.One = 1;
            Console.WriteLine(myClass.One); //write "1"
            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public MyClass()
        {
            Console.WriteLine("MyClass ctor called.");
        }

        public int One
        {
            get;
            set;
        }
    }
}

With .NET 8 right around the corner and the obsoletion of the FormatterService

You can use RuntimeHelpers.GetUninitializedObject, which is the same method that FormatterService.GetUninitializedObject was calling under the hood in the first place.

Sign up to request clarification or add additional context in comments.

8 Comments

Awesome, looks like that's exactly what I need. I assume uninitialized means all its memory will be set to zeros? (Similar to how structs are instantiated)
Whatever the default value is for each type will be the default. So objects will be null, ints 0, etc. I do think that any class-level initialization occurs, but no constructor is run.
@JSBangs, That sucks you are dinging a perfectly legitimate answer. Your comment and the other answer doesn't actually address the question asked. If you feel like you have a better answer then provide one. But the answer I provided highlight how to use a documented class in the same way other serialization classes use this code.
@JSBangs FormatterServices (msdn.microsoft.com/en-us/library/…) is not undocumented.
|
73

Use this overload of the CreateInstance method:

public static Object CreateInstance(
    Type type,
    params Object[] args
)

Creates an instance of the specified type using the constructor that best matches the specified parameters.

See: http://msdn.microsoft.com/en-us/library/wcxyzt4d.aspx

2 Comments

This solution oversimplifies the problem. What if I don't know my type and I'm saying "just create an object of the Type in this Type variable"?
@kamii if neither the type nor the parameters it expects are known then what can you do with that class?
26

When I benchmarked performance of (T)FormatterServices.GetUninitializedObject(typeof(T)) it was slower. At the same time compiled expressions would give you great speed improvements though they work only for types with default constructor. I took a hybrid approach:

public static class New<T>
{
    public static readonly Func<T> Instance = Creator();

    static Func<T> Creator()
    {
        Type t = typeof(T);
        if (t == typeof(string))
            return Expression.Lambda<Func<T>>(Expression.Constant(string.Empty)).Compile();

        if (t.HasDefaultConstructor())
            return Expression.Lambda<Func<T>>(Expression.New(t)).Compile();

        return () => (T)FormatterServices.GetUninitializedObject(t);
    }
}

public static bool HasDefaultConstructor(this Type t)
{
    return t.IsValueType || t.GetConstructor(Type.EmptyTypes) != null;
}

This means the create expression is effectively cached and incurs penalty only the first time the type is loaded. Will handle value types too in an efficient manner.

Call it:

MyType me = New<MyType>.Instance();

Note that (T)FormatterServices.GetUninitializedObject(t) will fail for string. Hence special handling for string is in place to return empty string.

3 Comments

It is strange how a look at one line of someone's code can save a day. Thank you, sir! Performance reasons took me to your post and the trick is done :) FormatterServices and Activator classes are underperforming compared to compiled expressions, what a pity one finds Activators all around the place.
@nawfal Regarding your special handling for string, I know it would fail for string without this special handling, but I just want to know: will it work for all other types?
@Sнаđошƒаӽ unfortunately no. The given example is barebones and .NET has many different types of types. For e.g. consider, if you pass a delegate type how will you give you it an instance? Or else if constructor throws what can you do about it? Many different ways to handle it. I had since answering this updated to handle lot more scenarios in my library. It isnt published anywhere for now.
4

Good answers but unusable on the dot net compact framework. Here is a solution that will work on CF.Net...

class Test
{
    int _myInt;

    public Test(int myInt)
    {
        _myInt = myInt;
    }

    public override string ToString()
    {
        return "My int = " + _myInt.ToString();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var ctor = typeof(Test).GetConstructor(new Type[] { typeof(int) });
        var obj = ctor.Invoke(new object[] { 10 });
        Console.WriteLine(obj);
    }
}

4 Comments

This is the way I'd call a non-default constructor. I'm not sure I'd ever want to create an object without calling any constructor at all.
You may want to create an object without calling constructors if you are writing custom serializers.
Yup, that's the exact use case scenario this question was for :)
@Aistina Perhaps you could add this information to the question? Most people would be against creating objects without calling their ctors and would take the time to argue with you about that, but your use case actually justifies it, so I think it is very relevant to the question itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.