开发者

How to retrieve the value of a number of properties?

开发者 https://www.devze.com 2023-03-17 18:00 出处:网络
I have a List collection that hold a number of property names. MyViewModel is a class 开发者_JAVA百科that holds all possible properties to be shown on a view.
  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

精彩评论

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