将枚举传递给函数
我想做的就是编写一个可以将任何枚举作为参数并打印该枚举的值的函数,但这似乎不可能:
例如,我想这样做:
void PrintEnumValues(Enum e) { ... }
enum Example1 { a, b }
enum Example2 { one, two }
PrintEnumValues(Example1);
PrintEnumValues(Example2);
这样的事情可能吗?
Possible Duplicate:
C# Iterating through an enum? (Indexing a System.Array)
All I want to do is write a function that can take any enum as a parameter and print the values of that enum but it doesn't seem possible:
For example, I'd like to do this:
void PrintEnumValues(Enum e) { ... }
enum Example1 { a, b }
enum Example2 { one, two }
PrintEnumValues(Example1);
PrintEnumValues(Example2);
Is something like this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能,但您需要制作 PrintEnumValues 的反射体。
possible but you need to make reflection body of PrintEnumValues.
您可以使用 Enum.GetValues() 并传递枚举的类型以获取值的数组。
You can use Enum.GetValues() and pass the type of the enum to get an array of the values.
这是我给您的建议:
像这样调用该函数:
Here is my suggestion for you:
Call that function like that:
然后将其用作:
Then use this as:
您在评论中说过
我不是在询问迭代
。所以我假设你想打印“a”:NOT
You said in your comment
I'm not asking about iterating
. So I assume you want to print "a":NOT