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

发布于 2024-10-17 15:27:55 字数 1013 浏览 3 评论 0原文

我正在尝试使用反射来列出一个 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());
        }
    }

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

Namespace.Subspace.event+EVENT_TYPE

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

public enum EVENT_TYPE
{
    EVENTDOWN,
    EVENTMOVE,
    EVENTUP,
}

应该输出类似这样的内容

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

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

谢谢

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:

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:

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

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

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

发布评论

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

评论(5

勿挽旧人 2024-10-24 15:27:55

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

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

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

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

君勿笑 2024-10-24 15:27:55

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

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

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();
                  }
              }
         }
    }
终弃我 2024-10-24 15:27:55

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

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

        string [] members;

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

    public enum numbers
    {
        one,
        two,
        three,
    }

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.

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

        string [] members;

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

    public enum numbers
    {
        one,
        two,
        three,
    }
旧城空念 2024-10-24 15:27:55

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

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

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.

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

百善笑为先 2024-10-24 15:27:55

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

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

    A = 0,
    B,
    C
}

然后这个

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

输出

A = 0
B = 1
C = 2

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

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