gpt4 book ai didi

c# - 获取 PropertyInfo 值

转载 作者:IT王子 更新时间:2023-10-29 04:52:46 27 4
gpt4 key购买 nike

我试图从 PropertyInfo[] 中获取值,但我无法让它工作:

foreach (var propertyInfo in foo.GetType().GetProperties())
{
var value = propertyInfo.GetValue(this, null);
}

Exception: Object does not match target type.

这不应该是这样吗?

最佳答案

当您最初从 foo.GetType() 获取 PropertyInfo 时,您正在尝试从 this 获取属性。所以这会更合适:

var value = propertyInfo.GetValue(foo, null);

假设您想要有效地获取 foo.SomeProperty

关于c# - 获取 PropertyInfo 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9081193/

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