How do you get the Value of a property from PropertyInfo?

后端 未结 4 1205
野性不改
野性不改 2020-12-16 09:58

I\'ve got an object that has a collection of properties. When I get the specific entity I can see the field I\'m looking for (opportunityid) and that it\'s

4条回答
  •  一生所求
    2020-12-16 10:20

    If the name of the property is changing, you should use GetValue:

    info.GetValue(attr, null);
    

    The last attribute of that method can be null, since it is the index value, and that is only needed when accessing arrays, like Value[1,2].

    If you know the name of the attribute on beforehand, you could use the dynamic behavior of it: you can call the property without the need of doing reflection yourself:

    var x = attr.Guid;
    

提交回复
热议问题