获得的PropertyInfo值 [英] Get propertyinfo value

查看:165
本文介绍了获得的PropertyInfo值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去得到一个的PropertyInfo [] 的价值,但我不能得到它的工作:

Im trying to get the value from a PropertyInfo[], but i cant get it to work:

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



例外:对象不匹配目标类型

心不是这怎么它应该做些什么呢?

Isnt this how its supposed to be done?

推荐答案

您正试图从属性本当您最初获取的的PropertyInfo 期从 foo.GetType()。因此,这将是更合适的:

You're trying to get properties from this when you originally fetched the PropertyInfos from foo.GetType(). So this would be more appropriate:

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

这是假设你想有效地获得 foo.SomeProperty 等。

That's assuming you want to effectively get foo.SomeProperty etc.

这篇关于获得的PropertyInfo值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆