程序等效默認(rèn)(類型)我使用反射循環(huán)通過(guò)一個(gè)Type屬性,并將某些類型設(shè)置為默認(rèn)類型。default(Type)很明顯,但我寧愿用一行來(lái)做。是否有相當(dāng)于默認(rèn)程序的程序?
3 回答

皈依舞
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
在值類型使用的情況下 它應(yīng)該能正常工作。 當(dāng)使用引用類型時(shí),只需返回NULL
public static object GetDefault(Type type){ if(type.IsValueType) { return Activator.CreateInstance(type); } return null;}
type.IsValueType
type.GetTypeInfo().IsValueType

小怪獸愛吃肉
TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
public object GetDefault(Type t) { return this.GetType().GetMethod("GetDefaultGeneric").MakeGenericMethod(t).Invoke(this, null); } public T GetDefaultGeneric<T>() { return default(T); }

楊__羊羊
TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊
PropertyInfo.SetValue(obj, null)
- 3 回答
- 0 關(guān)注
- 446 瀏覽
添加回答
舉報(bào)
0/150
提交
取消