获取枚举,而不必键入它的类名? [英] Getting the Enum without having to type its class name?

查看:98
本文介绍了获取枚举,而不必键入它的类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在C#中,当我有一个枚举,说:

I notice that in C#, when I have an Enum, say:

class ClassObject {
   public static Enum EventType = {Click, Jump, Etc};
}



而当我需要访问它,我必须去通过它的主类这是非常冗长。对于例如:

And when I have to access it, I have to go through it's main class and it is very verbose. For eg:

ClassObject.EventType.Click

我希望我能访问它在较短的方式,说:
EventType.Click ,将让我得到。该

I hope that I could access it in a shorter manner, say: EventType.Click, will allow me get that.

所以我想我可以尝试创建一个单独的类和枚举扩展它:

So what I thought I could try to create a separate class and extend it from Enum:

class EventType : Enum {
   Click, Jump, Etc;
}



不过,这我得到一个语法错误这一点。

However, this I'm getting a syntax error for this.

其实,只是为了这个目的创建另一个单独的类是有点麻烦,我甚至不知道这是不是一个很好的做法与否。但是,理想情况下,我只希望缩短名字,可能避免在访问枚举之前键入类名称的需要。这可能吗?

Actually, creating another separate class just for this purpose is a little troublesome and I don't even know if this is a good practice or not. But ideally, I just wish to shorten the name and probably avoid the need for having to type the class name before accessing the enum. Is this possible?

推荐答案

您可以把枚举在类的相同深度,它并没有成为一个类成员

You can put the enum at the same depth of the class, it doesn't have to be a class member

namespace Test
{
    public enum EventType
    {
        Click,
        Jump,
        Etc
    };

    class ClassObject { }
}

这篇关于获取枚举,而不必键入它的类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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