开发者

How to retrieve the value of a number of properties?

  1. I have a List collection that hold a number of property names.

  2. MyViewModel is a class 开发者_JAVA百科that holds all possible properties to be shown on a view.

  3. Now I would like go through a collection of MyViewModel and only get the values of all those properties that were defined previously in step 1).

I think I have to use reflection.

I could get the property name like this:

public static string GetPropertyName<T>(Expression<Func<T>> expression)
{
    var body = (MemberExpression) expression.Body;
    return body.Member.Name;
}

However how do I now utilise this further to go through the collection and get the values for only the properties I have defined in step 1?

Many thanks.


If you have a property name you need to do ( roughly - I cannot test it out ATM )

thing.GetType().Properties(propname).GetValue(thing,null);

which should return you the value of the 'propname' property on the 'thing' object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜