Question

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.

Was it helpful?

Solution

I think what you need is FieldInfo.

OTHER TIPS

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top