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

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

问题描述

  

可能重复:
  <一href="http://stackoverflow.com/questions/7244/anyone-know-a-good-workaround-for-the-lack-of-an-enum-generic-constraint">Anyone知道一个很好的解决方法缺乏一个枚举通用的限制呢?

什么是背后的C#不允许类型约束枚举的原因?我敢肯定有疯狂背后的方法,但我想知道为什么它是不可能的。

下面是什么,我想是能够做到(理论上)。

 公共静态牛逼GetEnum&LT; T&GT;(此字符串描述),其中T:枚举
{
...
}
 

解决方案

这是一个偶然要求的功能。

由于我乐于指出,所有的功能都得到执行,直到有人设计,规范,实施,测试,文档和船舶的功能。到目前为止,没有人做过,对于这一个。有没有特别不寻常的原因,原因何在;我们也有很多其他的事情要做,预算有限,而这一次也没有把它过去的这是不是很好吗?在语言设计小组的讨论。

<打击>的CLR不支持它,所以为了使其工作我们需要做的运行工作,除了语言的工作。(见回答评论)

我可以看到,有几个像样的使用情况,但他们都不是如此引人注目,我们会做这项工作,而不是数以百计的其他功能,都更加​​频繁请求,还是比较有吸引力,一更深远的使用情况。 (如果我们要淤泥与此code,我个人优先考虑委托约束的方式,上面枚举约束的方式。)

Possible Duplicate:
Anyone know a good workaround for the lack of an enum generic constraint?

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.

The CLR doesn't support it, so in order to make it work we'd need to do runtime work in addition to the language work. (see answer comments)

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天全站免登陆