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

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

问题描述


可能重复:






C#不支持 Enum 的类型约束是什么原因?我相信有一种疯狂背后的方法,但我想明白为什么不可能。



以下是我想要做的(理论上)

  public static T GetEnum< T>(此字符串描述)其中T:枚举
{
...
}


解决方案

这是偶尔要求的功能。



我喜欢指出,所有功能都没有实现,除非有人设计,规格,实施,测试,文档和发送功能。到目前为止,没有人为此做过。没有什么特别不寻常的原因我们还有很多其他事情要做,预算有限,而且这个事情从来没有超过这不会好吗?语言设计团队的讨论



CLR不支持它,所以为了使其工作,我们需要另外执行运行时工作我们可以看到有一些体面的用例,但没有一个是如此引人注目的,我们可以看到,做这个工作,而不是更多次要求的数百个其他功能之一,或者具有更强大和更深入的使用情况。 (如果我们要使用这个代码,我会亲自对代理约束的方式进行优先级排序,比上面的枚举约束更为方便。)


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