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");

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2021-12-13
  • 2022-02-03
  • 2022-01-04
相关资源
相似解决方案