如何将枚举数量作为常数? [英] How can I get the number of enums as a constant?

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

问题描述

枚举中定义的项目总数我看到我可以通过使用以下方式获取枚举数:

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;

非常棒!

但是,需要这个数字作为一个常数,所以我可以在Unity的 [Range(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?

推荐答案

不可能将枚举数作为 const 获取。 Enum.GetNames 使用反射来获取这些东西,这本质上是一个运行时操作。因此,在编译时无法知道,这是 const 的要求。

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屋!

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