C# CIL:什么是;“类”;关键字指定给";时的平均值;。字段“;?
我有一个字段定义:C# CIL:什么是;“类”;关键字指定给";时的平均值;。字段“;?,c#,.net,cil,C#,.net,Cil,我有一个字段定义: .field public static initonly class A.Program/'<>c' '<>9' 与 ILSpy将my.field public static initonly class System.Object'x'行转换为.field public static initonly Object x(省略class关键字,生成public static readonly Object x;C#)根据ECMA-335: 字段::
.field public static initonly class A.Program/'<>c' '<>9'
与
ILSpy将my.field public static initonly class System.Object'x'
行转换为.field public static initonly Object x
(省略class
关键字,生成public static readonly Object x;
C#)根据ECMA-335:
字段::=.Field
FieldDecl
FieldDecl::=
['['
Int32']
]FieldAttr*
类型Id['='
FieldInit| at
DataLabel]
FieldAttr::=
汇编
famandassem
系列
initonly
literal
封送(“
NativeType”)“
|未序列化
|私有
|编译器控制
|公共
|特殊名称
|静态
键入
::=
“!”代码>Int32 |“!!”
Int32bool
char
Int32class
TypeReferencefloat32
float64
int8
int16
Int32
int64
方法参数“
native int
native unsigned int
object
string
”&“
”
”
”[/code>[Bound[/code>,
Bound]](“
TypeReference”)“
| Typemodreq
”(“
TypeReference”)”
| Type
pinted
typedref
valuetype
TypeReference
unsigned int8
unsigned int16
unsigned int32
unsigned int64
void
TypeSpec::=
'['
.module
]DottedName']'
|类型参考
如您所见,class
不是字段声明的一部分,而是类型本身的一部分(TypeSpec可以在class
或valuetype
可以省略的其他地方使用)。我不能100%确定为什么在某些情况下必须使用class
或valuetype
,但也有boxed
TypeReference,因此可能最初也可以选择该类型的一种形式。您的字段定义来自哪里?它是.NET为例如Task.Run(()=>{Console.WriteLine(“hello world”);class
这里是名称class A.Program/'c'
的语法部分。请参阅第II.23.2.12节。字段public static initonly class A.Program/'c''9'
相当于public static readonly A.Program.c 9
(即c
类型的名为9
的字段,它是a.Program
的嵌套类型),当然,除了这些在c#中不是有效标识符之外。要在ILSpy中看到这一点,请选择“IL”或“IL with c#”作为它的输出类型;默认情况下,这些类型被透明地翻译回惯用的C#。谢谢@Jeroenmoster!如果你把它作为一个答案来写,我会接受的。我想你有点不走运,[object]是IL中的一个关键字。因此不需要类型通常获得的类注释。Reflection.Emit没有C#编译器用于翻译lambdas、async、迭代器等的代码重写逻辑。生成带有不稳定尖括号的隐藏类型的类型,您必须自己完成。
// Metadata version: v4.0.30319
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4..
.ver 4:0:0:0
}
.assembly Test
{
// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 03 01 00 00 00 00 )
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module Test
// MVID: {9110E73E-F37F-4E22-9D43-20F26D4A4C8F}
// --- The following custom attribute is added automatically, do not uncomment -------
// .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 03 01 00 00 00 00 )
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x0000029D41150000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi Test
extends [mscorlib]System.Object
{
.field public static initonly class System.Object 'x'
} // end of class Test
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
ilasm /dll /debug test.il