迭代枚举?
我正在尝试迭代枚举,并使用其每个值作为参数来调用方法。必须有一种比我现在所拥有的更好的方法来做到这一点:
foreach (string gameObjectType in Enum.GetNames(typeof(GameObjectType)))
{
GameObjectType kind = (GameObjectType) Enum.Parse(typeof (GameObjectType), gameObjectType);
IDictionary<string, string> gameObjectData = PersistentUtils.LoadGameObject(kind, persistentState);
}
//...
public static IDictionary<string, string> LoadGameObject(GameObjectType gameObjectType, IPersistentState persistentState) { /* ... */ }
将枚举名称作为字符串,然后将它们解析回枚举,感觉很可怕。
I'm trying to iterate over an enum, and call a method using each of its values as a parameter. There has to be a better way to do it than what I have now:
foreach (string gameObjectType in Enum.GetNames(typeof(GameObjectType)))
{
GameObjectType kind = (GameObjectType) Enum.Parse(typeof (GameObjectType), gameObjectType);
IDictionary<string, string> gameObjectData = PersistentUtils.LoadGameObject(kind, persistentState);
}
//...
public static IDictionary<string, string> LoadGameObject(GameObjectType gameObjectType, IPersistentState persistentState) { /* ... */ }
Getting the enum names as strings, then parsing them back to enums, feels hideous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,您可以使用
Enum.GetValues
:虽然它不是强类型的 - 而且 IIRC 它非常慢。另一种方法是使用我的 UnconstrainedMelody 项目:
如果您要做的是使用枚举进行了大量工作,但对于单次使用来说可能有点过分了......
Well, you can use
Enum.GetValues
:It's not strongly typed though - and IIRC it's pretty slow. An alternative is to use my UnconstrainedMelody project:
UnconstrainedMelody is nice if you're doing a lot of work with enums, but it might be overkill for a single usage...
以防万一其他人
疯狂到想要这样做想要在 C++/CLI 中执行此操作,这里有一个可以工作的端口:Just in case anyone else
is crazy enough to want to dowants to do this in C++/CLI, here's a port which works: