Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to get all Enum values in c#

I've tried a little Program... I want to run a program and see all method names in c# class... Here is the code

class Program
{
    public int adf()
    {
        return 0;
    }
    static void Main(string[] args)
    {

        foreach (MethodInfo mInfo in typeof(Program).GetMethods(BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
        {
            Console.WriteLine(mInfo.Name);
        }
       Console.ReadKey();
    }
    private void bdf()
    {
        Console.WriteLine("Dg");
    }
}

It's work fine, in result I've got this

 adf
 main
 bdf

Now , I want to pass to GetMethods function only one parameter and get result... I don't think it's a good way to pass 5 parameters with 'binary or(|)' ... In BindingFlags Enum is 19 fields and what it will be if I want to pass 18 parameters xD How can I do it passing only one value?

Here Is Enum

 public enum BindingFlags
{
    Default = 0,
    IgnoreCase = 1,
    DeclaredOnly = 2,
    Instance = 4,
    Static = 8,
    Public = 16,
    NonPublic = 32,
    FlattenHierarchy = 64,
    InvokeMethod = 256,
    CreateInstance = 512,
    GetField = 1024,
    SetField = 2048,
    GetProperty = 4096,
    SetProperty = 8192,
    PutDispProperty = 16384,
    PutRefDispProperty = 32768,
    ExactBinding = 65536,
    SuppressChangeType = 131072,
    OptionalParamBinding = 262144,
    IgnoreReturn = 16777216,
  }
}

I think it's very interesting and helpful question...

like image 951
Chuck Norris Avatar asked Aug 05 '11 10:08

Chuck Norris


2 Answers

Code below should get a value containing all flags (could easily be made into a generic method), you can then do AllFlags & ~FlagToRemove to get all but one flag.

AllFlags = (EnumFlagType)Enum.GetValues(typeof(EnumFlagType))
                             .Cast<int>().Aggregate((acc, next) => acc | next);

[Flags]
enum TestEnum { one = 1, two = 2, three = 4, four = 8 };

void Main()
{

    var AllFlags = (TestEnum)Enum.GetValues(typeof(TestEnum))
                             .Cast<int>().Aggregate((acc, next) => acc | next);

    Console.WriteLine(AllFlags); // Prints "one, two, three, four"

    Console.WriteLine(AllFlags & ~two); // Prints "one, three, four"
}
like image 155
George Duckett Avatar answered Sep 28 '22 18:09

George Duckett


Pay attantion at the body of BindingFlags enumeration, all the values are power of 2. So binary or just calculates sum of provided integer values. In order to pass all flags just send the sum of all int values. In order to pass only some values just send binary integer with 1 in the corresponding position of the flag which needs to be passed. Please see code below.

BindingFlags flag = (BindingFlags)Convert.ToInt32("0000010010000101010", 2)

for your example must be

BindingFlags flag = (BindingFlags)Convert.ToInt32("111110", 2)

and when we print the flag we have a

DeclaredOnly, Instance, Static, Public, NonPublic

and you can get metods

            Type t = typeof(Program);
            MethodInfo[] mi = t.GetMethods(flag);

You right, it is a really interesting question.

like image 23
Artur Keyan Avatar answered Sep 28 '22 18:09

Artur Keyan