我有这样的属性值类:如何使用MethodInfo.Invoke设置属性值?
public class MyClass {
public property var Value { get; set; }
....
}
我想用MethodInfo.Invoke()设置属性值。以下是一些代码:
object o;
// use CodeDom to get instance of a dynamically built MyClass to o, codes omitted
Type type = o.GetType();
MethodInfo mi = type.GetProperty("Value");
mi.Invoke(o, new object[] {23}); // Set Value to 23?
我现在无法访问我的工作。我的问题是如何设置一个整数值,如23值?
使用PropertyInfo.SetValue,如下所述。如果您发现您受限于使用MethodInfo对象,请获取该属性的“get”方法(PropertyInfo.GetGetMethod()),并如上所述调用它。 – 2009-07-01 06:06:39