在 C# 中使用反射列出枚举中的值
我正在尝试使用反射来列出一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
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).
如何通过反射获取枚举和值
how to get enum and values via reflection
因此,在您的情况下,检查源是否是枚举类型,然后调用 GetEnumNames() 将允许代码对类、枚举等进行操作。
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.
枚举被实现为公共静态只读字段(可能也是 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...
有一种方法可以用几行调用
然后这个
输出
There is a way to call in few lines
And then this
Output