与国旗方法扩展Enum? [英] Extend Enum with flag methods?
问题描述
我已经找到了如何创建扩展方法,从按位枚举读出单值很好的例子。但现在,C#4增加了真的不需要他们的HasFlag方法。
我觉得这是真正有用的,虽然是设置一个标志的延伸<! BR />
我有,我需要单独设置标志值很多情况下
我想与此签名扩展方法:
enumVariable.SetFlag(EnumType.SingleFlag,真);
或可能:
enumVariable.SetFlag< EnumType>(EnumType.SingleFlag,真);
今天,我发现上的 http://hugoware.net/blog/enums-flags-and-csharp 。感谢雨果!优秀的代码工作正常。我调整稍微并将其添加到现有的EnumExtender:
公共静态类EnumExtender
{
/ //<总结>
///将一个标志值来枚举。
///请注意,枚举是值类型,所以你需要处理此方法返回的值。
///举例:myEnumVariable = myEnumVariable.AddFlag(CustomEnumType.Value1);
///< /总结>
公共静态牛逼AddFlag< T>(该枚举类型,T enumFlag)
{
试
{
回报率(T)(对象)((INT)(对象)类型|(INT)(对象)enumFlag);
}
赶上(异常前)
{
抛出新的ArgumentException(的String.Format(无法追加标志值{0}以枚举{1},enumFlag的typeof (T).Name点),除息);
}
}
///<总结>
///移除枚举标志值。
///请注意,枚举是值类型,所以你需要处理此方法返回的值。
///举例:myEnumVariable = myEnumVariable.RemoveFlag(CustomEnumType.Value1);
///< /总结>
公共静态牛逼RemoveFlag< T>(该枚举类型,T enumFlag)
{
试
{
回报率(T)(对象)((INT)(对象)的类型和放大器;〜(INT)(对象)enumFlag);
}
赶上(异常前)
{
抛出新的ArgumentException(的String.Format(无法删除标志值{0}从枚举{1},enumFlag的typeof (T).Name点),除息);
}
}
///<总结>
///在枚举设置标志状态。
///请注意,枚举是值类型,所以你需要处理此方法返回的值。
///举例:myEnumVariable = myEnumVariable.SetFlag(CustomEnumType.Value1,真);
///< /总结>
公共静态牛逼SetFlag< T>(该枚举类型,T enumFlag,布尔值)
{
返回值? type.AddFlag(enumFlag):type.RemoveFlag(enumFlag);
}
///<总结>
///检查该标志值是相同的所提供的枚举。
///< /总结>
公共静态布尔IsIdenticalFlag< T>(该枚举类型,T enumFlag)
{
试
{
回报(INT)(对象)类型==(INT )(对象)enumFlag;
}
抓
{
返回FALSE;
}
}
///<总结>
///转换提供枚举类型值的列表。
///当你需要遍历枚举值非常方便。
///< /总结>
公共静态列表< T> &了ToList LT; T>()
{
如果
抛出新的ArgumentException()(typeof运算(T).IsEnum!);
VAR值= Enum.GetNames(typeof运算(T));
返回values.Select(价值=> value.ToEnum< T>())了ToList()。
}
///<总结>
///目前枚举值作为逗号分隔字符串。
///< /总结>
公共静态字符串的GetValues< T>()
{
如果
抛出新的ArgumentException()(typeof运算(T).IsEnum!);
VAR值= Enum.GetNames(typeof运算(T));
返回的string.join(,,值);
}
}
I have found good examples on how to create extension methods to read out single values from bitwise enums. But now that C# 4 has added the HasFlag method they are really not needed.
What I think would be really helpful though is an extension to SET a single flag!
I have many situations where I need to set the flag values individually.
I want an extension method with this signature:
enumVariable.SetFlag(EnumType.SingleFlag, true);
OR possibly:
enumVariable.SetFlag<EnumType>(EnumType.SingleFlag, true);
Today I found a solution on http://hugoware.net/blog/enums-flags-and-csharp. Thanks Hugo! Excellent code that works fine. I adjusted it slightly and added it to my existing EnumExtender:
public static class EnumExtender
{
/// <summary>
/// Adds a flag value to enum.
/// Please note that enums are value types so you need to handle the RETURNED value from this method.
/// Example: myEnumVariable = myEnumVariable.AddFlag(CustomEnumType.Value1);
/// </summary>
public static T AddFlag<T>(this Enum type, T enumFlag)
{
try
{
return (T)(object)((int)(object)type|(int)(object)enumFlag);
}
catch(Exception ex)
{
throw new ArgumentException(string.Format("Could not append flag value {0} to enum {1}",enumFlag, typeof(T).Name), ex);
}
}
/// <summary>
/// Removes the flag value from enum.
/// Please note that enums are value types so you need to handle the RETURNED value from this method.
/// Example: myEnumVariable = myEnumVariable.RemoveFlag(CustomEnumType.Value1);
/// </summary>
public static T RemoveFlag<T>(this Enum type, T enumFlag)
{
try
{
return (T)(object)((int)(object)type & ~(int)(object)enumFlag);
}
catch (Exception ex)
{
throw new ArgumentException(string.Format("Could not remove flag value {0} from enum {1}", enumFlag, typeof(T).Name), ex);
}
}
/// <summary>
/// Sets flag state on enum.
/// Please note that enums are value types so you need to handle the RETURNED value from this method.
/// Example: myEnumVariable = myEnumVariable.SetFlag(CustomEnumType.Value1, true);
/// </summary>
public static T SetFlag<T>(this Enum type, T enumFlag, bool value)
{
return value ? type.AddFlag(enumFlag) : type.RemoveFlag(enumFlag);
}
/// <summary>
/// Checks if the flag value is identical to the provided enum.
/// </summary>
public static bool IsIdenticalFlag<T>(this Enum type, T enumFlag)
{
try
{
return (int)(object)type == (int)(object)enumFlag;
}
catch
{
return false;
}
}
/// <summary>
/// Convert provided enum type to list of values.
/// This is convenient when you need to iterate enum values.
/// </summary>
public static List<T> ToList<T>()
{
if (!typeof(T).IsEnum)
throw new ArgumentException();
var values = Enum.GetNames(typeof(T));
return values.Select(value => value.ToEnum<T>()).ToList();
}
/// <summary>
/// Present the enum values as a comma separated string.
/// </summary>
public static string GetValues<T>()
{
if (!typeof(T).IsEnum)
throw new ArgumentException();
var values = Enum.GetNames(typeof(T));
return string.Join(", ", values);
}
}
这篇关于与国旗方法扩展Enum?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!