gpt4 book ai didi

c# - 下拉枚举

转载 作者:太空宇宙 更新时间:2023-11-03 19:30:00 24 4
gpt4 key购买 nike

我想将所选值的字符串输出从下拉列表转换为枚举。最好的方法是什么?

最佳答案

您可以将其包装到扩展方法中以简化调用:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com