在 C# 中获取所有 Enum 值的简单方法

发布于 2024-11-27 23:45:01 字数 1349 浏览 6 评论 0原文

我尝试了一个小程序...我想运行一个程序并查看 c# 类中的所有方法名称... 这是代码,

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

工作正常,结果我得到了这个

 adf
 main
 bdf

现在,我只想传递给 GetMethods 函数一个参数并获得结果...我不认为这是传递 5 个参数的好方法'binary or(|)' ... BindingFlags Enum 中有 19 个字段,如果我想传递 18 个参数,它会是什么 xD 我怎样才能做到只传递一个值?

这是枚举

 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'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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清晰传感 2024-12-04 23:45:01

下面的代码应该获取包含所有标志的值(可以轻松地制作成通用方法),然后您可以执行 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"
}

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"
}
云淡风轻 2024-12-04 23:45:01

请注意 BindingFlags 枚举的主体,所有值都是 2 的幂。因此二进制或仅计算提供的整数值的总和。为了传递所有标志,只需发送所有 int 值的总和。为了仅传递某些值,只需在需要传递的标志的相应位置发送带有 1 的二进制整数。请参阅下面的代码。

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

因为你的例子必须是

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

,当我们打印flag时,我们有一个

DeclaredOnly, Instance, Static, Public, NonPublic

,你可以得到方法

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

你是的,这是一个非常有趣的问题。

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.

心奴独伤 2024-12-04 23:45:01

编写静态方法:

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

Write static method:

public static class BindingFlagsHelper
{
    public static BindingFlags GetAllMethods()
    {
        return 
            BindingFlags.NonPublic | 
            BindingFlags.DeclaredOnly | 
            BindingFlags.Public | 
            BindingFlags.Instance | BindingFlags.Static;
    }
}
我喜欢麦丽素 2024-12-04 23:45:01

您可以使用变量来保存有趣的标志并将其传递:

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 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);
}
感情旳空白 2024-12-04 23:45:01

您不会传递 5 个参数,当按位或执行完成时,您将仅传递一个 int,您可以放置​​无限的标志和“|”并且只会传递一个 sizeof(int) 参数。 来生成预先计算的值

您可以使用const int my_val = flag_1|flag_3|flag_5;

You won't be passing 5 parameters, you'll be passing only one int when the bitwise ors are done executing, you could put unlimited flags and '|' them and only one sizeof(int) parameter will be passed. You could make precomputed values with

const int my_val = flag_1|flag_3|flag_5;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文