如何在CIL中声明值类型:`.class value`或只是`.class`? [英] How to declare a value type in CIL: `.class value` or just `.class`?

查看:129
本文介绍了如何在CIL中声明值类型:`.class value`或只是`.class`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ILDASM struct FooStruct a>,并看到以下内容:

I have taken a look at a C# struct FooStruct in ILDASM, and have seen the following:

ILDASM在这里显示两个不同的声明:

ILDASM here displays two differing declarations:

  • .class value public(后窗和前窗的标题栏)开头的
  • 一个仅以.class public(前窗口)开头的
  • one starting with .class value public (rear window & front window's title bar)
  • one starting with just .class public (front window)

我想知道哪种语法(如果不是全部)是用于声明值类型的正确语法? value修饰符是严格必要的,是可选的还是语法错误?

And I wonder which syntax (if not both) is the correct one for declaring a value type? Is the value modifier strictly necessary, or optional, or a syntax error?

推荐答案

简短答案::值类型定义仅需extends [mscorlib]System.ValueTypevalue属性似乎是可选的,没有明显作用.

Short answer: Value type definitions only require extends [mscorlib]System.ValueType; the value attribute appears to be optional and has no apparent effect.

我认为CLI规范( ECMA-335 )将是寻找权威答案的最佳位置.

I assume that the CLI specification (ECMA-335) would be the best place to look for an authorative answer.

第II.10节处理定义类型.更具体地说,第II.10.1.3小节规定:

Section II.10 deals with defining types. More specifically, subsection II.10.1.3 states:

类型语义属性指定是否应定义接口,类或值类型. …如果[interface]属性不存在并且定义扩展 (直接或间接)System.ValueType,并且该定义不适用于System.Enum,则应定义值类型(第II.13节).否则,应定义一个类(第II.11节).

The type semantic attributes specify whether an interface, class, or value type shall be defined. … If [the interface] attribute is not present and the definition extends (directly or indirectly) System.ValueType, and the definition is not for System.Enum, a value type shall be defined (§II.13). Otherwise, a class shall be defined (§II.11).

在整个部分中根本没有提到value属性.

The value attribute is not mentioned at all in the whole section.

结论:正确的值类型定义不必包含value.从System.ValueType派生就足够了.

Conclusion: A correct value type definition does not have to include value. Deriving from System.ValueType is sufficient.

CLI标准还包含ILASM的语法(在VI.C.3节中).根据该语法,对于.class类型定义存在一个value属性.我还在标准中搜索了具体的值类型定义,并找到了以下示例:

The CLI standard also contains a grammar for ILASM (in section VI.C.3). According to that grammar, there exists a value attribute for .class type definitions. I additionally searched the standard for concrete value type definitions and found these examples:

  • .class public sequential ansi serializable sealed beforefieldinit System.Double extends System.ValueType …
  • .class private sealed Rational extends [mscorlib]System.ValueType …
  • .class value sealed public MyClass extends [mscorlib]System.ValueType …
  • .class public sequential ansi serializable sealed beforefieldinit System.Double extends System.ValueType …
  • .class private sealed Rational extends [mscorlib]System.ValueType …
  • .class value sealed public MyClass extends [mscorlib]System.ValueType …

结论:值类型定义中可能包含value属性.

Conclusion: A value attribute may be included in a value type definition.

我试图将这三个IL类型定义编译为一个程序集:

I tried to compile these three IL type definitions into an assembly:

.class       public sealed … A extends [mscorlib]System.ValueType { … }
.class value public sealed … B extends [mscorlib]System.ValueType { … }
.class value public sealed … C extends [mscorlib]System.Object    { … }  // !!!

即使在引用类型声明中使用了value属性,也没有编译错误(请参阅最后一行).使用Visual Studio 2012的对象浏览器查看生成的程序集,将发现两种值类型(struct)AB,以及一种引用类型(class)C.

There was no compilation error, even though the value attribute is used in a reference type declaration (see last line). Looking at the resulting assembly using Visual Studio 2012's Object Browser reveals two value types (struct) A and B, and one reference type (class) C.

推测: value属性的存在对类型定义没有任何影响.它只是对人类发现值类型定义的潜在帮助.

Speculation: The presence of the value attribute has no effect whatsoever on the type definition. It is only there as a potential aid for humans in spotting value type definitions.

这篇关于如何在CIL中声明值类型:`.class value`或只是`.class`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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