Set Enum Value with reflection .Net1.1 and .Net2.0
While trying to set value for enum via Reflection I discovered .Net2.0 is much smarter in setting values.
In .Net2.0 the following works:
Int32 property = int.Parse("20");
if (propertyInfo.CanWrite)
{
this.propertyInfo.SetValue(ParentPage.TheProperties, property, null);
}
but this same code as above will not work in .Net1.1 and will throw a cast exception. Here is the workaround for .Net1.1, where we explicitly do Enum.ToObject(...) . ie.
object property = int.Parse(ddProperty.SelectedValue);
object newEnumValue = Enum.ToObject(propertyInfo.PropertyType, property);
if (propertyInfo.CanWrite)
{
this.propertyInfo.SetValue(ParentPage.TheProperties, newEnumValue, null);
}