我怎样才能为一个常数枚举数? [英] How can I get the number of enums as a constant?
问题描述
在一个枚举定义的项目总数,我看我可以用得到枚举数:
From Total number of items defined in an enum, I see I can get the number of enums by using:
Enum.GetNames(typeof(Item.Type)).Length;
伟大工程!
Works great!
不过,我需要这个号码作为常数,这样我可以在统一的 [范围(INT,INT)]
功能使用它。
But, I need this number as a constant number, so that I can use it in Unity's [Range(int, int)]
feature.
private const int constEnumCount = Enum.GetNames(typeof(Item.Type)).Length;
以上不工作,因为枚举数不是一个恒定的数字,所以我不能给它分配一个常数变量。
The above does not work, because the enum count is not a constant number, so I cannot assign it to a constant variable.
我怎样才能得到枚举数为常量?
How can I get the number of enums as a constant?
推荐答案
这是不可能得到枚举数为常量
。 Enum.GetNames
使用反射来得到这些东西,这是本质上运行时操作。因此,它不能在编译时,这是一个要求为是常量
称。
It's not possible to get the number of enums as a const
. Enum.GetNames
uses reflection to get these things, and that's inherently a runtime operation. Therefore, it can't be known at compile time, which is a requirement for being const
.
这篇关于我怎样才能为一个常数枚举数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!