我试图从a获得价值PropertyInfo[],但我无法让它工作:
foreach (var propertyInfo in foo.GetType().GetProperties())
{
var value = propertyInfo.GetValue(this, null);
}
Run Code Online (Sandbox Code Playgroud)
例外: Object does not match target type.
这不应该怎么做?
Jon*_*eet 31
您试图从this最初获取PropertyInfos 时获取属性foo.GetType().所以这更合适:
var value = propertyInfo.GetValue(foo, null);
Run Code Online (Sandbox Code Playgroud)
假设你想要有效地获得foo.SomeProperty等等.
你得到了那个例外,因为this它的类型不同foo.
您应确保获取要尝试从中获取值的同一对象的属性.我猜测你的代码中你期望这个在循环范围内是foo(根本不是这种情况),所以你需要将违规行更改为:
var value = propertyInfo.GetValue(foo, null);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22883 次 |
| 最近记录: |