问题
我正在尝试动态获取在 ParameterInfo 中指定的类型的默认值。 _methods[methodName] 返回一个 MethodInfo 对象。
不幸的是,编译器不喜欢 default(paramType) 中的“paramType”位。我很难过。
错误
The type or namespace name 'paramType' could not be found (are you missing a using directive or an assembly reference?)
C:\Applications\...\MessageReceiver.cs Line 113
例子
object blankObject = null;
foreach (var paramInfo in _methods[methodName].Key.GetParameters())
{
if (paramInfo.Name == paramName)
{
Type paramType = paramInfo.ParameterType;
blankObject = (object)default(paramType);
}
}
parameters[i] = blankObject;
最佳答案
实现起来非常简单:
public object GetDefault(Type type)
{
return type.IsValueType ? Activator.CreateInstance(type) : null;
}
关于c# - 动态获取参数类型的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/474841/