在泛型中返回一个新实例而不是null实例

dea*_*vmc 4 c# generics isolatedstorage windows-phone-7

我有一个从隔离存储中提取对象的类.如果它找不到有问题的对象,则返回默认值(T),该值为null,因为它们是引用类型.如果返回的值为null,我会进行简单检查并在调用者中分配一个新实例,但我更愿意在存储逻辑中执行此操作.

所以我的问题是,有没有办法返回一个新的T,其中对象有一个默认的空白构造函数?

The*_*aot 14

一个选项是使用约束"新":http: //msdn.microsoft.com/en-us/library/sd2w2ew5(v=vs.80).aspx

像这样:

public T GetNewItem()
    where T: new()
{
    return new T();
}
Run Code Online (Sandbox Code Playgroud)

但是具有此约束意味着您不能使用没有默认构造函数的类型.因此,您可以考虑使用System.Activator.CreateInstance,但请记住它可能会抛出异常:

T createInstance<T>()
{
    try
    {
        return System.Activator.CreateInstance<T>();
    }
    catch (MissingMethodException exc)
    {
        return default(T);
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,在初始化的早期知道给定类型是否支持这一点可能是一个好主意,一种方法如下:

T createInstance<T>()
{
    System.Reflection.ConstructorInfo constructor = (typeof(T)).GetConstructor(System.Type.EmptyTypes);
    if (ReferenceEquals(constructor, null))
    {
        //there is no default constructor
        return default(T);
    }
    else
    {
        //there is a default constructor
        //you can invoke it like so:
        return (T)constructor.Invoke(new object[0]);
        //return constructor.Invoke(new object[0]) as T; //If T is class
    }
}
Run Code Online (Sandbox Code Playgroud)

当你在这里时,为什么不让一个创建实例的委托?

Func<T> getConstructor<T>()
{
    System.Reflection.ConstructorInfo constructor = (typeof(T)).GetConstructor(System.Type.EmptyTypes);
    if (ReferenceEquals(constructor, null))
    {
        return () => { return default(T); };
    }
    else
    {
        return () => { return (T)constructor.Invoke(new object[0]); };
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用它的示例(使用LinqPad编译):

void Main()
{
    Console.WriteLine(getConstructor<object>()());
    Console.WriteLine(getConstructor<int>()());
    Console.WriteLine(getConstructor<string>()());
    Console.WriteLine(getConstructor<decimal>()());
    Console.WriteLine(getConstructor<DateTime>()());
    Console.WriteLine(getConstructor<int?>()());
}
Run Code Online (Sandbox Code Playgroud)

输出是:

System.Object
0
null
0
01/01/0001 12:00:00 a.m.
null
Run Code Online (Sandbox Code Playgroud)

string的情况是一种特殊情况,它是一个引用类型,它可以为null,并且没有公共默认构造函数,而不是String.Empty.可空类型也给出null.