我知道“default”关键字返回静态确定类型的默认值,例如this question。如何以非泛型方式获取某个类型的默认值?
但是,给定一个类型的实例,是否有一种简单的方法来动态获取此类型的默认值? 我发现谷歌搜索,而唯一的办法就是this:
static object DefaultValue(Type myType)
{
if (!myType.IsValueType)
return null;
else
return Activator.CreateInstance(myType);
}
,但我想如果可能的话,避免Activator类。
欢迎世界表达? http://stackoverflow.com/questions/6582259/fast-creation-of-objects-instead-of-activator-createinstancetype :) – nawfal 2013-04-24 12:05:44