Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to get the value from a PropertyInfo[], but I can't get it to work:

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

Exception: Object does not match target type.

Isn't this how it's supposed to be done?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
119 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...