设置Nullable<T>类型对象的值为default(T)

public T CreateDefault()
{
    T item = new T();

    foreach (var p in typeof(T).GetProperties())
    {
        Type colType = p.PropertyType;

        if (colType == typeof(Nullable<DateTime>))
        {
            p.SetValue(item, DateTime.Now, null);
        }
        else if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
            var t = Nullable.GetUnderlyingType(colType);
            p.SetValue(item, Activator.CreateInstance(t), null);
        }
    }

    return item;
}
原文地址:https://www.cnblogs.com/ChobitsSP/p/8759260.html