我想在一個Visual Studio解決方案中的各個項目中使用反射來列出公共成員和幾個類的方法。我試圖訪問的所有類都是C#,它們都是從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
任何幫助,任何人都可以提供將不勝感激。我已經用盡了迄今爲止所能找到的一切,但全新的視角將會很好。
感謝
[@Craigt答案](http://stackoverflow.com/questions/5005931/listing-values-within-enums-using-reflection-in-c/5006004#5006004)可能在你的上下文中更準確。看看它。 – 2011-02-15 16:23:14