程序等效默认(类型)我使用反射循环通过一个Type属性,并将某些类型设置为默认类型。default(Type)很明显,但我宁愿用一行来做。是否有相当于默认程序的程序?
3 回答
皈依舞
TA贡献1851条经验 获得超3个赞
在值类型使用的情况下 它应该能正常工作。 当使用引用类型时,只需返回NULL
public static object GetDefault(Type type){ if(type.IsValueType) { return Activator.CreateInstance(type); } return null;}
type.IsValueType
type.GetTypeInfo().IsValueType
小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
public object GetDefault(Type t) { return this.GetType().GetMethod("GetDefaultGeneric").MakeGenericMethod(t).Invoke(this, null); } public T GetDefaultGeneric<T>() { return default(T); }
杨__羊羊
TA贡献1943条经验 获得超7个赞
PropertyInfo.SetValue(obj, null)
- 3 回答
- 0 关注
- 422 浏览
添加回答
举报
0/150
提交
取消