How to get an instance's member's values?
With propertyInfos there is a propertyInfo.GetValue(instance, index)
, but no such thing exists in memberInfo.
I searched the net, but it seems to stop at getting the member's name and type.
You have to downcast to FieldInfo
or PropertyInfo
:
switch (memberInfo)
{
case FieldInfo fieldInfo:
return fieldInfo.GetValue(obj);
case PropertyInfo propertyInfo:
return propertyInfo.GetValue(obj);
default:
throw new InvalidOperationException();
}
I think what you need is FieldInfo
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With