// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public static class FlagHack { public static bool IsFlagDefined(this Enum en) { int allbits = 0; foreach(int bit in Enum.GetValues(en.GetType())) { allbits |= bit; } int val = Convert.ToInt32(en); return (val & allbits) == val; } } [Flags] public enum X { X1=1, X2=2, X3=4 }; [Flags] public enum Y { Y1=1, Y2=8, Y3=64 }; public class Program { public static void Main(string[] args) { X ox = X.X2; Console.WriteLine(ox.IsFlagDefined()); ox = X.X1 | X.X3; Console.WriteLine(ox.IsFlagDefined()); ox = (X)8; Console.WriteLine(ox.IsFlagDefined()); Y oy = Y.Y2; Console.WriteLine(oy.IsFlagDefined()); oy = Y.Y1 | Y.Y3; Console.WriteLine(oy.IsFlagDefined()); oy = (Y)3; Console.WriteLine(oy.IsFlagDefined()); } } }