C#中的枚举类型约束 [英] Enum type constraints in C#

查看:25
本文介绍了C#中的枚举类型约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
任何人都知道一个好的解决方法缺少枚举通用约束?

C# 不允许对 Enum 进行类型约束的原因是什么?我确信疯狂背后有一种方法,但我想了解为什么这是不可能的.

What is the reason behind C# not allowing type constraints on Enum's? I'm sure there is a method behind the madness, but I'd like to understand why it's not possible.

以下是我希望能够做到的(理论上).

Below is what I would like to be able to do (in theory).

public static T GetEnum<T>(this string description) where T : Enum
{
...
}

推荐答案

这是一个偶尔需要的功能.

This is an occasionally requested feature.

正如我喜欢指出的那样,在有人设计、规范、实施、测试、记录和交付该功能之前,所有功能都未实现.到目前为止,还没有人为这一点做到这一点.没有什么特别不寻常的原因.我们还有很多其他的事情要做,预算有限,而这件事从来没有超过这不是很好吗?"语言设计团队中的讨论.

As I'm fond of pointing out, ALL features are unimplemented until someone designs, specs, implements, tests, documents and ships the feature. So far, no one has done that for this one. There's no particularly unusual reason why not; we have lots of other things to do, limited budgets, and this one has never made it past the "wouldn't this be nice?" discussion in the language design team.

CLR 不支持它,所以为了让它工作,除了语言工作之外,我们还需要做运行时工作.(见答案评论)

我可以看到有一些不错的使用案例,但没有一个如此引人注目,以至于我们会做这项工作,而不是其他数百个更频繁请求的功能中的一个,或者更引人注目和更深远的用例.(如果我们要弄乱这段代码,我个人会优先考虑委托约束方式,远高于枚举约束.)

I can see that there are a few decent usage cases, but none of them are so compelling that we'd do this work rather than one of the hundreds of other features that are much more frequently requested, or have more compelling and farther-reaching usage cases. (If we're going to muck with this code, I'd personally prioritize delegate constraints way, way above enum constraints.)

这篇关于C#中的枚举类型约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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