C#的枚举函数参数? [英] C# enums as function parameters?

查看:122
本文介绍了C#的枚举函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以通过一个标准的C#枚举作为参数?

Can you pass a standard c# enum as a parameter?

例如:

enum e1
{
    //...
}

enum e2
{
    //...
}

public void test()
{
    myFunc( e1 );
    myFunc( e2 );
}

public void myFunc( Enum e )
{
    // Iterate through all the values in e
}

通过这样做,我希望能找回任何给定的枚举中的所有名称。会是什么样的迭代代码样子?

By doing this I hope to retrieve all the names within any given enum. What would the Iteration code look like?

推荐答案

这!

        public void Foo(Enum e)
        {
            var names = Enum.GetNames(e.GetType());

            foreach (var name in names)
            {
                // do something!
            }
        }



编辑:我的坏,你的没有< 。/ em>的说的迭代

注:我知道,我可能只是做GetNames()调用在我的foreach语句,但我更喜欢首先分配给方法调用类型的东西,因为它很方便进行调试。

Note: I know I could just do the GetNames() call in my foreach statement, but I prefer to assign that type of thing to a method call first, as it's handy for debugging.

这篇关于C#的枚举函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆