编译器错误CS0702 [英] Compiler Error CS0702

查看:96
本文介绍了编译器错误CS0702的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的希望能够将泛型类型约束到System.Enum,或者

更好,枚举。但当然会导致编译器错误CS0702。


到目前为止,我一直在运行时检查类型参数并抛出

异常如果提供的类型不是枚举。这是有效的,但它只是

看起来不太正确。


阅读完C#3.0规范的4.4.4后,我认为没有理由为什么

限制在System.Enum不应该被允许。


所以我的问题是:

是否有这些类型的原因不能或不应该支持

约束?

(如果是这样,编译器错误CS0702的文档应该说明它们。)

这只是编译器的限制而不是语言的限制吗?

是否正在努力支持这些类型作为约束?

I''d really like to be able to constrain a generic type to System.Enum or,
better, enum. But of course that results in "Compiler Error CS0702".

So far I''ve been checking the type parameter at runtime and throwing an
exception if the provided type is not an enum. That works, but it just
doesn''t seem quite right.

After reading through section 4.4.4 of the C# 3.0 spec I see no reason why
constraining to System.Enum should not be allowed.

So my questions are:
Are there reasons why these types can''t or shouldn''t be supported as
constraints?
(If so, the documentation of Compiler Error CS0702 ought to state them.)
Is this simply a limitation of the compiler rather than of the language?
Are efforts being to made to support these types as constraints?

推荐答案

2008年4月17日星期四09:31:03 -0700,PIEBALD

< PI ***** @ discussion.microsoft.comwrote :
On Thu, 17 Apr 2008 09:31:03 -0700, PIEBALD
<PI*****@discussions.microsoft.comwrote:

我真的希望能够将泛型类型约束到System.Enum,或者

更好,枚举。但当然会导致编译器错误CS0702。


到目前为止,我一直在运行时检查类型参数并抛出

异常如果提供的类型不是枚举。这是有效的,但它只是

看起来不太正确。


阅读完C#3.0规范的4.4.4后,我认为没有理由

为什么

不允许约束System.Enum。
I''d really like to be able to constrain a generic type to System.Enum or,
better, enum. But of course that results in "Compiler Error CS0702".

So far I''ve been checking the type parameter at runtime and throwing an
exception if the provided type is not an enum. That works, but it just
doesn''t seem quite right.

After reading through section 4.4.4 of the C# 3.0 spec I see no reason
why
constraining to System.Enum should not be allowed.



我想我对C#版和

规格版之间的关系感到有点困惑。


我正在看标准ECMA-334,第4版,2006年6月。没有

"第4.4.4节。我正在查看的文档描述了第405页的第25.7节中的通用

约束。它描述了允许的

约束,以清楚地排除枚举或其他值类型。允许的唯一

主要约束是实际参考(类)类型,或

关键字class类型。或者结构。


什么让你觉得规范允许

enum作为类型约束?

I guess I''m a bit confused about the relationship between C# version and
specification version.

I''m looking at "Standard ECMA-334", 4th edition, June 2006. There''s no
"section 4.4.4". The document I''m looking at describes generic
constraints on page 405, section 25.7. And it describes the allowable
constraints to clearly exclude enums or other value types. The only
primary constraints allowed are an actual reference (class) type, or the
keywords "class" or "struct".

What is giving you the impression that the specification would allow an
enum as a type constraint?


所以我的问题是:

有没有理由说明为什么这些类型不能或不应该被支持为

约束?
So my questions are:
Are there reasons why these types can''t or shouldn''t be supported as
constraints?



嗯,我认为最明显的原因是因为价值类型不能继续其他价值类型,如果你''重新限制类只允许一个特定的值类型,然后首先不需要该类是通用的

。你只能用一种特定类型的通用类




由于枚举是一种值类型,它会落入同样的推理。

Well, I think the most obvious reason is that since value types can''t
inherit other value types, if you''re constraining the class to allow only
one specific value type, then there''s no need for the class to be generic
in the first place. You''d only ever be able to use that generic class
with one specific type.

Since an enum is a value type, it would fall into that same reasoning.


(如果是这样,编译器错误CS0702的文档应该说明它们。)

这只是一个限制编译器而不是语言?
(If so, the documentation of Compiler Error CS0702 ought to state them.)
Is this simply a limitation of the compiler rather than of the language?



见上文。当我读到它时,这显然被描述为语言的一部分。

See above. As I read it, this is clearly described as part of thelanguage.


是否正在努力支持这些类型作为约束?
Are efforts being to made to support these types as constraints?



据我所知。鉴于上面的

,我不明白为什么会做出任何努力。


Pete

Not to my knowledge. I don''t see why any effort would be made, given the
above.

Pete


2008年4月17日星期四09:57:50 -0700,Peter Duniho

< Np ********* @nnowslpianmk.comwrote:
On Thu, 17 Apr 2008 09:57:50 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:

[...]
[...]

>在阅读C#3.0规范的4.4.4节之后,我看到没有理由<为什么不应该允许约束System.Enum。
>After reading through section 4.4.4 of the C# 3.0 spec I see no reason
why
constraining to System.Enum should not be allowed.



我想我对C#版和

规格版之间的关系感到有点困惑。


我正在看标准ECMA-334,第4版,2006年6月。没有

"第4.4.4节。我正在查看的文档描述了第405页的第25.7节中的通用

约束。它描述了允许的

约束,以清楚地排除枚举或其他值类型。允许的唯一

主要约束是实际参考(类)类型,或

关键字class类型。或结构。


I guess I''m a bit confused about the relationship between C# version and
specification version.

I''m looking at "Standard ECMA-334", 4th edition, June 2006. There''s no
"section 4.4.4". The document I''m looking at describes generic
constraints on page 405, section 25.7. And it describes the allowable
constraints to clearly exclude enums or other value types. The only
primary constraints allowed are an actual reference (class) type, or the
keywords "class" or "struct".



好​​的......我找到了C#3.0规范。我猜ECMA还没有采用它

呢?我在他们的网站上的任何地方都没有看到它。


无论如何,当_using_

为泛型时,4.4.4节与satistfying约束有关。我不知道这对于你可以为一般通用声明的

限制是否相关。


这让我回到了我的身边上一个问题:

Okay...I found the C# 3.0 specification. I guess ECMA hasn''t adopted it
yet? I don''t see it anywhere on their web site.

Anyway, section 4.4.4 has to do with satistfying constraints when _using_
a generic. I don''t see how that''s relevant with respect to what
constraints you can declare for a generic.

Which brings me back to my previous question:


什么让你觉得规范允许

enum作为类型约束?
What is giving you the impression that the specification would allow an
enum as a type constraint?



具体来说,你能引用C#规范中的任何文本

直接导致你相信你应该能够指定一个枚举

类型作为通用的约束?


Pete

Specifically, can you quote any text from the C# specification that
directly leads you to believe that you should be able to specify an enum
type as a constraint for a generic?

Pete


我正在看标准ECMA-334,第4版,2006年6月。没有


我正在查看C#语言规范版本3.0。可在
http://msdn2.microsoft获取。 com / zh-CN / vcsharp / aa336809.aspx

I''m looking at "Standard ECMA-334", 4th edition, June 2006. There''s no

I''m looking at the "C# Language Specification Version 3.0" available at
http://msdn2.microsoft.com/en-us/vcsharp/aa336809.aspx


这篇关于编译器错误CS0702的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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