我想将所选值的字符串输出从下拉列表转换为枚举。 最好的方法是什么?
最佳答案
您可以将其包装到扩展方法中以简化调用:
public static T ToEnum<T>(this string value) {
if (string.IsNullOrWhiteSpace(value)) {
throw new ArgumentNullException("Cannot convert null or empty string to an enum");
}
// Get enum from the built-in Parse method
return (T)Enum.Parse(typeof(T), value, true);
}
然后调用
myValue.ToEnum<EnumNameHere>();
获取枚举
关于c# - 下拉枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5623315/