windows 在 C# 中使用反射列出枚举中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5005931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:11:34  来源:igfitidea点击:

Listing values within enums using reflection in C#

c#windowsreflectionenums

提问by RyanMullins

I am trying to use reflection to list the public members and methods of a few classes in various projects inside of one Visual Studio solution. All of the classes I am trying to access are C# and they are all being accessed from a C# class. The code I'm using to make these calls is as follows:

我正在尝试使用反射来列出一个 Visual Studio 解决方案内各种项目中几个类的公共成员和方法。我试图访问的所有类都是 C# 并且它们都是从 C# 类访问的。我用来进行这些调用的代码如下:

public void PopulateEventParamTree()
    {
        System.Console.WriteLine(source.GetType().ToString());
        Type type = (Type)source.getEventType();
        System.Console.WriteLine(type.ToString());

        foreach (MemberInfo member in type.GetMembers())
        {
            System.Console.WriteLine("\t" + member.ToString());
        }
    }

Most of the outputs work fine (i.e. Int32, Double, System.String). My problem is that when I try to list enums I get an output to the console that looks like this:

大多数输出​​工作正常(即 Int32、Double、System.String)。我的问题是,当我尝试列出枚举时,我得到的控制台输出如下所示:

Namespace.Subspace.event+EVENT_TYPE

I would like to be able to see all of the inner values of the enum instead of just its name. For example, the enum

我希望能够看到枚举的所有内部值,而不仅仅是它的名称。例如,枚举

public enum EVENT_TYPE
{
    EVENTDOWN,
    EVENTMOVE,
    EVENTUP,
}

should output something like this

应该输出这样的东西

Namespace.Subspace.class+EVENT_TYPE EVENTDOWN
Namespace.Subspace.class+EVENT_TYPE EVENTMOVE
Namespace.Subspace.class+EVENT_TYPE EVENTUP

Any help that anyone can provide would be greatly appreciated. I've exhausted everything I've been able to find thus far but a fresh perspective would be nice.

任何人都可以提供的任何帮助将不胜感激。到目前为止,我已经用尽了我能找到的所有东西,但如果有新的视角会很好。

Thanks

谢谢

回答by Steve B

Use System.Enum.GetNames(typeof(EVENT_TYPE)).

使用System.Enum.GetNames(typeof(EVENT_TYPE)).

you will probably have to deal this special case (not using reflection for enums).

您可能必须处理这种特殊情况(不对枚举使用反射)。

回答by Daniel Pamich

how to get enum and values via reflection

如何通过反射获取枚举和值

var importAssembly = System.Reflection.Assembly.LoadFile("test.dll");     
Type[] types = importAssembly.GetTypes();
     foreach (Type type in types)
        {
        if (type.IsEnum)
        {
               var enumName=type.Name;
               foreach (var fieldInfo in type.GetFields())
               {
                  if (fieldInfo.FieldType.IsEnum)
                  {
                      var fName=fieldInfo.Name;
                      var fValue=fieldInfo.GetRawConstantValue()
                  }
              }
         }
    }

回答by Marc Gravell

The enums are implemented as public static readonly fields (probably also const); your current code should work... You just need to get the name from the FieldInfo. And call GetValue if you want the value.

枚举被实现为公共静态只读字段(可能也是 const);您当前的代码应该可以工作...您只需要从 FieldInfo 中获取名称。如果需要该值,请调用 GetValue。

However, Enum.GetValues(type) is easier...

然而, Enum.GetValues(type) 更容易......

回答by Craigt

So in your case checking if source is an enum type and then calling GetEnumNames() would allow the code to act on classes, enums etc.

因此,在您的情况下,检查 source 是否为枚举类型,然后调用 GetEnumNames() 将允许代码对类、枚举等进行操作。

    private void Work()
    {
        var type = typeof(numbers);

        string [] members;

        if(type.IsEnum)
            members = typeof(numbers).GetEnumNames();
    }

    public enum numbers
    {
        one,
        two,
        three,
    }

回答by T.S.

There is a way to call in few lines

有一种方法可以在几行中调用

public enum TestEnum // in some assembly there is some enum
{

    A = 0,
    B,
    C
}

And then this

然后这个

var assembly = Assembly.Load("TestCS");
var converter = new EnumConverter(assembly.GetType("TestCS.TestEnum"));
var enumValColl = converter.GetStandardValues();

foreach (var val in enumValColl)
    Debug.WriteLine("{0} = {1}", val, (int)val);

Output

输出

A = 0
B = 1
C = 2

A = 0
B = 1
C = 2