Reflection question

  • Thread starter Thread starter Fred
  • Start date Start date
F

Fred

Hi

I need to loop through the properties of an object and record the various
values. I have some reflection code that does this nicely but any enumerated
properties return the Text of the enumeration not the underlying value. The
code is:

FormElement _Element = new FormElement("Testing123");
Type _Type = _Element.GetType();
PropertyInfo[] _Members = _Type.GetProperties();

for (short c = 0; c < _Members.Length; c++)
{

object _Test = _Members[c].GetValue(_Element, null);
Type _Type = _Members[c].PropertyType;

if (_Type.IsEnum)
{
// get underlying value
}
else
{

// OK to use this
writer.WriteElementString(_Members[c].Name, _Test.ToString());
}

}

I know there should be a way to look up the relevant enum value - but can't
work it out. Any offers anyone?

Thanks

David Billingham
 
Use this if it is enum:

Console.WriteLine(Convert.ChangeType(_Test,
Enum.GetUnderlyingType(_Type)));
 
Back
Top