views:

190

answers:

3

If I want a method that returns the default value of a given type and the method is generic I can return a default value like so:

public static T GetDefaultValue()
{
  return default(T);
}

Can I do something similar in case I have the type only as a System.Type object?

public static object GetDefaultValue(Type type)
{
  //???
}
+1  A: 

Without a generic, you can't guarantee that the type has a parameterless constructor, but you can search for one using reflection:

public static object GetDefaultValue(Type type)
{
    ConstructorInfo ci = type.GetConstructor( new Type[] {} );
    return ci.Invoke( new object[] {} );
}

I tried this in a console app, and it returns a "default" instance of the class — assuming it's a class. If you need it to work for reference types as well, you'll need an additional technique.

harpo
THe default value for reference type is null, and not an instance of the class. So if he does what you suggest GetDefault(T) will return null while GetDefault(Type) will try to make an instance if possible, which is wrong. You technique is not useless but I guess it is more like a "T GetInstance(T) where T : new() {return new T();}" type of method.
JohannesH
I see what you mean. Thanks for the correction!
harpo
+16  A: 

Since you really only have to worry about value types (reference types will just be null), you can use Activator.CreateInstance to call the default constructor on them.

public static object GetDefaultValue(Type type) {
   return type.IsValueType ? Activator.CreateInstance(type) : null;
}

Edit: Jon is (of course) correct. IsClass isn't exhaustive enough - it returns False if type is an interface.

Mark Brackett
Better to use !type.IsValueType, to cope with interfaces.
Jon Skeet
thanks guys! that's what I was looking for!
Patrick Klug
A: 

Here is how I normally do it. This avoids the whole 'IsValueType' or searching for constructors issues altogether.

public static object MakeDefault(this Type type)
{
    var makeDefault = typeof(ExtReflection).GetMethod("MakeDefaultGeneric");
    var typed = makeDefault.MakeGenericMethod(type);
    return typed.Invoke(null, new object[] { });
}

public static T MakeDefaultGeneric<T>()
{
    return default(T);
}
jonfuller