How can I get a value of a property from an anonymous type?

后端 未结 12 1257
南旧
南旧 2020-12-24 08:34

I have a datagrid populated by a Linq query. When the focused row in the datagrid changes I need to set a variable equal to one of the properties in that object.

I t

12条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 09:20

    You can loop through the properties of an anonymous type like this:

    var obj = new {someValue = "hello", otherValue = "world"};
    foreach (var propertyInfo in obj.GetType().GetProperties() {
        var name = propertyInfo.Name;
        var value = propertyInfo.GetValue(obj, index: null);
        ...
    }
    

提交回复
热议问题