是什么让 ValueType 类特别? [英] What makes ValueType class Special?

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

问题描述

当我尝试定义一个继承自 System.ValueTypeSystem.Enum 类的类时,我收到一个错误:

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

我理解那个错误,但我不明白是什么使 ValueType特殊?我的意思是没有关键字(如 sealed)或属性来指定这个类不能被继承.ValueType 有两个属性,SerializableComVisible 但它们都与这种情况无关.文档 说:

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:

尽管 ValueType 是值类型的隐式基类,您不能创建直接从 ValueType 继承的类. 相反,个别编译器提供语言关键字或构造(例如 C# 中的 struct 和 Structure…在 Visual Basic 中结束结构)以支持创建值类型.

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.

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

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?

同样所有结构implicitly继承自ValueType,但EnumExplicitly继承自ValueType,那么它是如何工作的?编译器怎么搞定这种情况的,所有这些都是编译器硬编码的?

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?

推荐答案

我明白那个错误,但我不明白是什么让 ValueType 类特别?

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.

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

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

是的.

所有结构都隐式继承自 ValueType,但 Enum 类显式继承自 ValueType,那么它是如何工作的呢?

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

效果很好.

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

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

是的.

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

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