Unless the property is static
, it is not enough to get a PropertyInfo
object to get a value of a property. When you write "plain" C# and you need to get a value of some property, say, MyProperty
, you write this:
var val = obj.MyProperty;
You supply two things - the property name (i.e. what to get) and the object (i.e. from where to get it).
PropertyInfo
represents the "what". You need to specify "from where" separately. When you call
var val = info.GetValue(obj);
you pass the "from where" to the PropertyInfo
, letting it extract the value of the property from the object for you.
Note: prior to .NET 4.5 you need to pass null as a second argument:
var val = info.GetValue(obj, null);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…