Myl*_*ell 9 .net c# reflection
什么是反射相当于:
default(object); //null
Run Code Online (Sandbox Code Playgroud)
当我在运行时之前没有类型时,例如
public void Method(Type type)
{
var instance = type.CreateDefault(); //no such method exists, but I expect there is a way of doing this?
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 14
对于任何引用类型,默认值为空实例.对于任何值类型,可以通过获取默认值Activator.CreateInstance.但是当你有一个名为的变量时instance,你会想要一个实际的实例而不是一个null引用...所以你可以这样做:
public object GetDefaultValue(Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
Run Code Online (Sandbox Code Playgroud)
......这不是很清楚它有多有用.这是类型的默认值,与默认的类型实例不同.
| 归档时间: |
|
| 查看次数: |
5225 次 |
| 最近记录: |