什么使ValueType类特殊? [英] What makes ValueType class Special?

查看:131
本文介绍了什么使ValueType类特殊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图定义继承自 System.ValueType System.Enum 类的类时,发生错误:

 无法从特殊类System.ValueType派生
pre>

我理解错误,但我不能理解是什么使 ValueType class special ?我的意思是没有关键字(如 sealed )或属性来指定这个类不能被继承。 ValueType 两个属性, Serializable ComVisible ,但是没有一个与这种情况相关。文档说:


虽然ValueType是值类型的隐式基类,但不能创建直接继承自ValueType的类。而是个别编译器提供一个语言关键字或构造(例如Visual Basic中的C#和Structure ... End Structure中的struct)以支持创建值类型。


没有回答我的问题。我的问题是如何通知编译器在这种情况下?当我尝试创建一个从类继承的类时,编译器直接检查类是 ValueType 还是 Enum



编辑:所有结构隐含继承自 ValueType Enum 显式继承自 ValueType ,那么如何工作?编译器如何确定这种情况,所有这些都是由编译器硬编码的?

解决方案


理解错误,但我不能理解是什么使ValueType类特别?


该类被记录为 是特殊的。这是什么使它特别。


在这种情况下如何通知编译器?


编译器编写者在编写编译器之前阅读文档。


当我尝试创建一个继承自类的类时,编译器是否直接检查类是ValueType还是Enum?


是的。


此外,所有结构都隐含地继承自ValueType,但Enum class显式继承自ValueType, ?


效果很好。


所有这些特殊情况都是硬编码到编译器中的?


是的。



< blockquote>

不是更适合创建一个属性来指定这个类是特殊的,不能继承而不是硬编码?


不,不是。这意味着第三方也可以做一个特殊的类型,当编译器继承时需要特殊的处理。那么第三方如何修改编译器来实现这些规则呢?


When I trying to define a class which inherits from System.ValueType or System.Enum class, I'm getting an error:

Cannot derive from special class System.ValueType

I understand that error but what I couldn't understand is what makes ValueType class special? I mean there is no keyword (like sealed) or attribute to specify that this class can not be inherited.ValueType has two attributes, Serializable and ComVisible but none of them is relevant with that case.The documentation says:

Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types.

But it doesn't answer my question.So my question is how the compiler is informed in this case? Does the compiler directly check whether the class is ValueType or Enum when I try to create a class that inherit from a class?

Edit: Also all structs implicitly inherit from ValueType, but Enum class Explicitly inherit from ValueType,so how is that working? How the compiler figure out this situation, all of this are hard coded by compiler?

解决方案

I understand that error but what I couldn't understand is what makes ValueType class special?

The class is documented as being special. That's what makes it special.

how the compiler is informed in this case?

The compiler writers read the documentation before they write the compiler.

Does the compiler directly check whether the class is ValueType or Enum when I try to create a class that inherit from a class?

Yes.

Also all structs implicitly inherit from ValueType, but Enum class Explicitly inherit from ValueType, so how is that working?

It's working pretty well.

Are all of these special cases hard coded into the compiler?

Yes.

Isn't it more appropriate to create an attribute to specify that this class is special and cannot be inherited instead of hard coding?

No, it's not. That would imply that a third party could also make a special type that needed special handling by the compiler when inherited from. How would the third party then modify the compiler to implement those rules?

这篇关于什么使ValueType类特殊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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