C# 有没有更好的方法来创建通用的将字符串转换为枚举方法或枚举扩展名?
我在enum助手类中有以下方法(为了回答这个问题,我对其进行了简化): 所以这里的问题是,我可以把它缩减为一个枚举扩展方法,或者是某种可以处理任何类型的单一方法。我已经找到了一些使用基本枚举的示例,但是我的示例中的不同之处在于,如果字符串为null或空(如果没有找到匹配项,我希望它失败),所有枚举都会返回C# 有没有更好的方法来创建通用的将字符串转换为枚举方法或枚举扩展名?,c#,enums,extension-methods,C#,Enums,Extension Methods,我在enum助手类中有以下方法(为了回答这个问题,我对其进行了简化): 所以这里的问题是,我可以把它缩减为一个枚举扩展方法,或者是某种可以处理任何类型的单一方法。我已经找到了一些使用基本枚举的示例,但是我的示例中的不同之处在于,如果字符串为null或空(如果没有找到匹配项,我希望它失败),所有枚举都会返回未知项 正在寻找类似以下内容的内容: EnumType1 value = EnumType1.Convert("Yes"); // or EnumType1 value = EnumHelper
未知
项
正在寻找类似以下内容的内容:
EnumType1 value = EnumType1.Convert("Yes");
// or
EnumType1 value = EnumHelper.Convert(EnumType1, "Yes");
一个功能可以完成这一切。。。如何处理未知
元素是我一直关注的问题
编辑:将其中一个枚举调整为不使用整数定义。因此,我可以保证0始终是正确的,但
未知
始终是正确的文本。。。我想我可以使用与T(0)相同的示例,但对文本“Unknown”进行另一次解析。使用泛型。。。像这样的
public static TResult ConvertTo<TResult>( this string source )
{
if( !typeof(TResult).IsEnum )
{
throw new NotSupportedException( "TResult must be an Enum" );
}
if (!Enum.GetNames(typeof(TResult)).Contains(source))
return default(TResult);
return (TResult)Enum.Parse( typeof(TResult), source );
}
public static TResult ConvertTo(此字符串源)
{
if(!typeof(TResult).IsEnum)
{
抛出新的NotSupportedException(“TResult必须是枚举”);
}
如果(!Enum.GetNames(typeof(TResult)).Contains(source))
返回默认值(TResult);
return(TResult)Enum.Parse(typeof(TResult),source);
}
()使用此选项,假设Unknown始终是0值
public static T ConvertToEnum<T>(this string value) where T : new()
{
if( !typeof(T).IsEnum )
throw new NotSupportedException( "T must be an Enum" );
try
{
return (T)Enum.Parse(typeof(T), value);
}
catch
{
return default(T); // equivalent to (T)0
//return (T)Enum.Parse(typeof(T), "Unknown"));
}
}
public static T ConvertToEnum(此字符串值),其中T:new()
{
if(!typeof(T).IsEnum)
抛出新的NotSupportedException(“T必须是枚举”);
尝试
{
返回(T)Enum.Parse(typeof(T),value);
}
抓住
{
返回默认值(T);//等价于(T)0
//返回(T)Enum.Parse(typeof(T),“未知”);
}
}
用法:
EnumType2 a = "Cat".ConvertToEnum<EnumType2>();
EnumType2 b = "Person".ConvertToEnum<EnumType2>(); // Unknown
enumtype2a=“Cat.ConvertToEnum();
EnumType2 b=“Person”.ConvertToEnum();//不为人知
由OP编辑(Kelsey):您的答案引导我找到正确的答案,因此我想我会将其包括在这里:
public static T ConvertTo<T>(this string value)
{
T returnValue = (T)(Enum.Parse(typeof(T), "Unknown", true));
if ((string.IsNullOrEmpty(value) == false) &&
(typeof(T).IsEnum))
{
try { returnValue = (T)(Enum.Parse(typeof(T), value, true)); }
catch { }
}
return returnValue;
}
public static T ConvertTo(此字符串值)
{
T returnValue=(T)(Enum.Parse(typeof(T),“未知”,true));
if((string.IsNullOrEmpty(value)=false)和
(T.IsEnum的类型)
{
尝试{returnValue=(T)(Enum.Parse(typeof(T),value,true));}
捕获{}
}
返回值;
}
我知道该怎么做…但是当EnumTypeX.Unknown
为null或空时,我如何获取它?添加此行。。。如果(!Enum.GetNames(typeof(TResult)).Contains(source))返回默认值(TResult);约束不能是特殊类“System.Enum”@Maud'Dib:Ugh,你说得对。我在我的虚拟项目中没有看到错误。我会更新。@Kelsey:你说得对,你每次都可以简单地重新分析“未知”,但是如果从0进行强制转换对你的枚举是安全的,则效率更高。(T)0
不编译:“无法将类型“int”转换为“T”。谢谢,我已经在你的答案中编辑了我将使用的内容,因为你的答案帮助了我。
public static T ConvertTo<T>(this string value)
{
T returnValue = (T)(Enum.Parse(typeof(T), "Unknown", true));
if ((string.IsNullOrEmpty(value) == false) &&
(typeof(T).IsEnum))
{
try { returnValue = (T)(Enum.Parse(typeof(T), value, true)); }
catch { }
}
return returnValue;
}