A week or so ago I came across this post in microsoft.public.dotnet.languages.csharp asking about using Generics to make dealing with bitwise operations on enums that have the FlagsAttribute. I was thinking that even though you can't use an Enum as Generic Constraint you can probably fudge it just enough to make a usefull class. So I posted my answer to the question and then decided to write my Enum<T> class. Yes, I actually named the class Enum<T>.
Enum.cs (17.12 kb)
public class Class1
{
[Flags]
public enum FlaggedEnum
{
None = 0,
Red = 1,
Blue = 2,
White = 4,
Black = 8,
Green = 16
}
public enum MyTestEnum
{
[Description("This is a description")]
Val0,
Val1,
Val2
}
static void Main(string[] args)
{
MyTestEnum a;
Enum<MyTestEnum>.TryParse("Val1", out a);
Debug.WriteLine(a); // output: Val1
bool returnValue = Enum<MyTestEnum>.TryParse("Val3", out a);
Debug.WriteLine(returnValue); // output: False
MyTestEnum? b;
Enum<MyTestEnum>.TryParse("Val2", out b);
Debug.WriteLine(b); // output: Val2
Enum<MyTestEnum>.TryParse("Val3", out b);
Debug.WriteLine(b); // output: nothing
//Enum<MyTestEnum>.Flags.IsFlagSet(a, MyTestEnum.Val2); // raises an assert by design
//int test;
//Enum<int>.TryParse("hello", out test); // raises an assert by design
// Flags Methods
FlaggedEnum flagged = FlaggedEnum.Red | FlaggedEnum.Blue | FlaggedEnum.White;
Debug.WriteLine(flagged); // output: Red, Blue, White
flagged = Enum<FlaggedEnum>.Flags.SwapFlag(flagged, FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Blue, White
flagged = Enum<FlaggedEnum>.Flags.SwapFlag(flagged, FlaggedEnum.Red);
Debug.WriteLine(flagged); // output: Red, Blue, White
flagged = Enum<FlaggedEnum>.Flags.ClearFlag(flagged, FlaggedEnum.White);
Debug.WriteLine(flagged); // output: Red, Blue
flagged = Enum<FlaggedEnum>.Flags.SetFlag(flagged, FlaggedEnum.White);
Debug.WriteLine(flagged); // output: Red, Blue, White
// using the ref overrides
Enum<FlaggedEnum>.Flags.SwapFlag(ref flagged, FlaggedEnum.White);
Debug.WriteLine(flagged); // output: Red, Blue
Enum<FlaggedEnum>.Flags.SetFlag(ref flagged, FlaggedEnum.White);
Debug.WriteLine(flagged); // output: Red, Blue, White
IList<int> values = Enum<FlaggedEnum>.GetValues<int>();
string name = Enum<FlaggedEnum>.GetName(2);
Debug.WriteLine(name); // output: Blue
DescriptionAttribute attrib = Enum<MyTestEnum>.Reflection.GetAttributeOnValue<DescriptionAttribute>(MyTestEnum.Val0);
Debug.WriteLine(attrib.Description); // output: This is a description
// For WinForms DataBinding
IList<IBindableEnum<MyTestEnum>> list = Enum<MyTestEnum>.DataBinding.CreateList();
foreach( IBindableEnum<MyTestEnum> bindable in list )
Debug.WriteLine(String.Format("{0} : {1}", bindable.Name, bindable.Value));
// output:
// Val0 : Val0
// Val1 : Val1
// Val2 : Val2
list = Enum<MyTestEnum>.DataBinding.CreateList
(
delegate(MyTestEnum target)
{
return target.ToString().Replace("Val", "Value ");
}
);
foreach (IBindableEnum<MyTestEnum> bindable in list)
Debug.WriteLine(String.Format("{0} : {1}", bindable.Name, bindable.Value));
// output:
// Value 0 : Val0
// Value 1 : Val1
// Value 2 : Val2
}
}