将枚举传递给函数

发布于 2024-12-20 02:42:17 字数 464 浏览 3 评论 0原文

可能的重复:
C# 迭代枚举? (索引 System.Array)

我想做的就是编写一个可以将任何枚举作为参数并打印该枚举的值的函数,但这似乎不可能:

例如,我想这样做:

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 技术交流群。

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

发布评论

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

评论(6

合约呢 2024-12-27 02:42:17
class Program
{
    enum Example1 { a, b }
    enum Example2 { one, two }

    static void Main()
    {
        PrintEnumValues(default(Example1));
        PrintEnumValues(default(Example2));
    }

    static void PrintEnumValues(Enum e) 
    {
        foreach (var item in Enum.GetValues(e.GetType()))
        {
            Console.WriteLine(item);         
        }
    }
}
class Program
{
    enum Example1 { a, b }
    enum Example2 { one, two }

    static void Main()
    {
        PrintEnumValues(default(Example1));
        PrintEnumValues(default(Example2));
    }

    static void PrintEnumValues(Enum e) 
    {
        foreach (var item in Enum.GetValues(e.GetType()))
        {
            Console.WriteLine(item);         
        }
    }
}
-黛色若梦 2024-12-27 02:42:17

可能,但您需要制作 PrintEnumValues 的反射体。

...
PrintEnumValues(typeof(Example1));
...


void PrintEnumValues(Type type)
{
 //sample: Enum.GetNames(type)
 //sample: Enum.GetValues(type)
}

possible but you need to make reflection body of PrintEnumValues.

...
PrintEnumValues(typeof(Example1));
...


void PrintEnumValues(Type type)
{
 //sample: Enum.GetNames(type)
 //sample: Enum.GetValues(type)
}
捎一片雪花 2024-12-27 02:42:17

您可以使用 Enum.GetValues() 并传递枚举的类型以获取值的数组。

You can use Enum.GetValues() and pass the type of the enum to get an array of the values.

天冷不及心凉 2024-12-27 02:42:17

这是我给您的建议:

void PrintEnumValues<TEnum>() 
{
    if (!typeof(Enum).IsAssignableFrom(typeof(TEnum)))
    {
        return;
    }
    foreach (var name in Enum.GetNames(typeof(TEnum)))
    {
        // Print name
    }
}

像这样调用该函数:

PrintEnumValues<Example1>(); 
PrintEnumValues<Example2>(); 

Here is my suggestion for you:

void PrintEnumValues<TEnum>() 
{
    if (!typeof(Enum).IsAssignableFrom(typeof(TEnum)))
    {
        return;
    }
    foreach (var name in Enum.GetNames(typeof(TEnum)))
    {
        // Print name
    }
}

Call that function like that:

PrintEnumValues<Example1>(); 
PrintEnumValues<Example2>(); 
叫嚣ゝ 2024-12-27 02:42:17
    enum Example1 
    { a, b }
    enum Example2 
    { one, two }

    public T[] GetEnumMembers<T>() where T : struct
    {
        T[] values = (T[])Enum.GetValues(typeof(T));
        return values;
    }

然后将其用作:

    var values = GetEnumMembers<Example1>();
    enum Example1 
    { a, b }
    enum Example2 
    { one, two }

    public T[] GetEnumMembers<T>() where T : struct
    {
        T[] values = (T[])Enum.GetValues(typeof(T));
        return values;
    }

Then use this as:

    var values = GetEnumMembers<Example1>();
若有似无的小暗淡 2024-12-27 02:42:17

您在评论中说过我不是在询问迭代。所以我假设你想打印“a”:

PrintEnumValues(Example1.a);

NOT

PrintEnumValues(Example1);

You said in your comment I'm not asking about iterating. So I assume you want to print "a":

PrintEnumValues(Example1.a);

NOT

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