简体   繁体   English

在c#中获取所有枚举值的简便方法

[英]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 我试过一个小程序...我想运行一个程序,看看c#class中的所有方法名称......这是代码

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? 现在,我想传递给GetMethods函数只有一个参数并得到结果......我认为这不是用'二进制或(|)'传递5个参数的好方法...在BindingFlags枚举是19个字段和什么如果我想传递18个参数xD我怎么能只传递一个值呢?

Here Is Enum 这是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... 我认为这是非常有趣和有用的问题......

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 & ~FlagToRemove来获得除一个标志之外的所有标志。

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"
}

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. 在BindingFlags枚举的主体上付费,所有的值都是2的幂。所以二进制或只计算提供的整数值的总和。 In order to pass all flags just send the sum of all int values. 为了传递所有标志,只需发送所有int值的总和。 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. 为了只传递一些值,只需在需要传递的标志的相应位置发送带有1的二进制整数。 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 当我们打印flag我们有一个

DeclaredOnly, Instance, Static, Public, NonPublic

and you can get metods 你可以得到metods

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

You right, it is a really interesting question. 对,这是一个非常有趣的问题。

Write static method: 写静态方法:

public static class BindingFlagsHelper
{
    public static BindingFlags GetAllMethods()
    {
        return 
            BindingFlags.NonPublic | 
            BindingFlags.DeclaredOnly | 
            BindingFlags.Public | 
            BindingFlags.Instance | BindingFlags.Static;
    }
}

You can use a variable to hold the interesting flags and pass that through: 您可以使用变量来保存有趣的标志并将其传递给:

BindingFlags myFlags = BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;

foreach (MethodInfo mInfo in typeof(Program).GetMethods(myFlags))
{
     Console.WriteLine(mInfo.Name);
}

You won't be passing 5 parameters, you'll be passing only one int when the bitwise or s are done executing, you could put unlimited flags and '|' 你不会传递5个参数,当bitwise or s执行完你时你只会传递一个int ,你可以放置无限的标志和'|' them and only one sizeof(int) parameter will be passed. 它们只传递一个sizeof(int)参数。 You could make precomputed values with 您可以使用以下方式生成预先计算的值

const int my_val = flag_1|flag_3|flag_5;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM