enum TypeName
{
SystemString,
SystemInt16,
SystemInt32
}
现在有string typeName 里面存放 TypeName的枚举值,比如“SystemString”
现在要取出TypeName.SystemString:
使用Enum类的反射:
(TypeName)typevalue = (TypeName)Enum.Parse( typeof (TypeName), typeName, true );
为了事先验证typename是否是TypeName的值,代码优化为:
if (Enum.IsDefined( typeof (TypeName), typeName))
typeValue = (TypeName)Enum.Parse( typeof (TypeName), typeName, true );
else
throw new Exception( " the return type does not defined " );