如何获得在C#中枚举类型的全局访问? [英] How to get global access to enum types in C#?
问题描述
这可能是一个愚蠢的问题,但我似乎无法做到这一点。我想成立一个类中的一些枚举是这样的:
This is probably a stupid question, but I can't seem to do it. I want to set up some enums in one class like this:
public enum Direction { north, east, south, west };
然后有那么一些其他类可以例如具有可访问到所有类,枚举类型:
Then have that enum type accessible to all classes so that some other class could for instance have:
Direction dir = north;
和能够通过阶级之间的枚举类型:
and be able to pass the enum type between classes:
public void changeDirection(Direction direction) {
dir = direction;
}
我认为枚举设置公共将使这个自动可能的,但似乎没有被我宣布在枚举类的外部可见的。
I thought that setting the enum to public would make this automatically possible, but it doesn't seem to be visible outside of the class I declared the enum in.
推荐答案
您可以做两件事情之一。
You can do one of two things.
1 - 将枚举的声明外部类
1- Move the declaration of the enum outside of the class
今天,你可能有这样的事情。
Today you probably have something like this
public class ClassName
{
public enum Direction
{
north, south, east, west
}
// ... Other class members etc.
}
这将改变为
public class ClassName
{
// ... Other class members etc.
}
// Enum declared outside of the class
public enum Direction
{
north, south, east, west
}
2 - 使用类名
2- Reference the enum using the class name
ClassName.Direction.north
例如:
public void changeDirection(ClassName.Direction direction) {
dir = direction;
}
其中,类名
是你宣布在枚举类的名称。
Where ClassName
is the name of the class that you declared the enum in.
这篇关于如何获得在C#中枚举类型的全局访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!