You have to use reflection.
To get the value of a property on targetObject
:
var value = targetObject.GetType().GetProperty(vari).GetValue(targetObject, null);
To get the value of a field it's similar:
var value = targetObject.GetType().GetField(vari).GetValue(targetObject, null);
If the property/field is not public or it has been inherited from a base class, you will need to provide explicit BindingFlags
to GetProperty
or GetField
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…