使用 Reflections 我需要从 ENums 中设置值

Using Reflections i need to set value from ENums

我有下面指定的枚举

  public enum AdvisorType {

    CAB,

    LA,


    RSL,
}

我的呼唤XML会是这样

<AdvisorType>CAB</AdvisorType>. 

我有一个可以为此对象设置值的解析器。我知道连载很简单,但我不做连载

if (prop.PropertyType.IsEnum)
                                {
                                    object o1 = Convert.ChangeType(childElement.Value, typeof(Object));
                                    Object valueSet = Enum.ToObject(prop.PropertyType, o1);
                                    prop.SetValue(obj, valueSet, null);
                                }
                                else
                                {
                                    prop.SetValue(obj, Convert.ChangeType(childElement.Value, prop.PropertyType), null);
                                    break;
                                }

Enum.Toobject 作为 "The value passed in must be an enum base or an underlying type for an enum, such as an Int32.".

抛出异常

像这样更改它并且有效

if (prop.PropertyType.IsEnum)
                                {
                                    Object valueSet = Enum.Parse(prop.PropertyType, childElement.Value, true);
                                    prop.SetValue(obj, valueSet, null);
                                }
                                else
                                {
                                    prop.SetValue(obj, Convert.ChangeType(childElement.Value, prop.PropertyType), null);
                                    break;
                                }