gpt4 book ai didi

c# - 如何使用 MethodInfo.Invoke 设置属性值?

转载 作者:太空狗 更新时间:2023-10-29 19:59:15 24 4
gpt4 key购买 nike

我有一个具有如下属性值的类:

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?

我现在无法访问我的工作 VS。我的问题是如何将 Value 设置为整数值,例如 23?

最佳答案

您可以使用 PropertyInfo.SetValue方法。

object o;
//...
Type type = o.GetType();
PropertyInfo pi = type.GetProperty("Value");
pi.SetValue(o, 23, null);

关于c# - 如何使用 MethodInfo.Invoke 设置属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1067312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com