Hi all,
I have a problem with an Enum and Reflection. I am using an Xml and
Reflection to create some controls, and to set their properties. All
goes well until I encounter one property which is defined like this:
public enum MyType {
Alpha = 0,
Beta = 1
}
public class MyClass {
private MyType _type;
public MyType theType {
get {
return _type;
}
set {
_type = value;
}
}
}
In another place I am using reflection to use a MyClass instance.
....
Control _control = (Control)
Assembly.LoadWithPartialName(assembly).CreateInsta nce(controlType);
Type type = _control.GetType();
PropertyInfo pi = type.GetProperty(prop.name);
pi.SetValue(_control, convertValue2PropertyType(valueToMap,
pi.PropertyType), null);
.....
the convertValue2PropertyType works fine on a lot of property types,
it does something like:
private object convertValue2PropertyType(string propValue, Type
propType)
{
if (propType.FullName == "System.Int16")
return System.Convert.ToInt16(propValue);
.....
and so on
.....
}
But when the propType.BaseType is System.Enum I cannot cast from the
propValue(which is an integer) to the proper MyType.
So if I have propValue == 0, I would like to be able to return
MyType.Alpha - Of course, in a programatic way, something like:
if (propType.BaseType.FullName == "System.Enum")
{
return (propType) System.Convert.ToInt32(propValue);
}
How do I achieve this?
TIA!
Alicia