我需要通过反射为属性赋值,到目前为止测试的每个案例都可以,除了类型为System.Reflection.Missing
. 例如这是一个类:
public class SomeClass
{
public System.Reflection.Missing MissingProp { get; set; }
}
这是通过反射分配给属性的代码:
var inst = new SomeClass();
var prop = typeof (SomeClass).GetProperty("MissingProp");
prop.SetValue(inst, Missing.Value, null);
这是我收到的例外情况:
System.ArgumentException: Missing parameter does not have a default value.
Parameter name: parameters
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at ClassesWithNoProperties.Program.Main(String[] args) in D:\Documents\Documents\Visual Studio 2010\Projects\C#\ClassesWithNoProperties\ClassesWithNoProperties\Program.cs:line 24
我已经用太多案例测试了我的库,这些案例使用相同的代码为属性赋值,这是System.Reflection.Missing
我观察到这种行为的唯一案例 ()。
如何在 C# 中完成这种任务?