I have a field definition:
.field public static initonly class A.Program/'<>c' '<>9'
This is part of a nested type generated by the C# compiler. I would like to construct something similar using ILGenerator.Emit.
Using ILDasm/ILasm, I built the following code in the hope it would give me the C#:
// 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 ***********************
with
ilasm /dll /debug test.il
ILSpy translates my .field public static initonly class System.Object 'x' line to .field public static initonly object x (omitting the class keyword and resulting in public static readonly object x; C#)
Per ECMA-335:
Field
::= .fieldFieldDeclFieldDecl
::=[‘[’Int32‘]’] FieldAttr*Type Id [‘=’FieldInit| atDataLabel ]FieldAttr
::=assembly|famandassem|family|famorassem|initonly|literal|marshal‘(’NativeType‘)’|notserialized|private|compilercontrolled|public|rtspecialname|specialname|static
Type::=‘!’Int32 |‘!!’Int32 |bool|char|classTypeReference |float32|float64|int8|int16|int32|int64|methodCallConv Type‘*’‘(’Parameters‘)’|native int|native unsigned int|object|string| Type‘&’| Type‘*’| Type‘<’GenArgs‘>’| Type‘[’[ Bound [‘,’Bound ]*]‘]’| Typemodopt‘(’TypeReference‘)’| Typemodreq‘(’TypeReference‘)’| Typepinned|typedref|valuetypeTypeReference |unsigned int8|unsigned int16|unsigned int32|unsigned int64|voidTypeSpec
::=‘[’[.module] DottedName‘]’| TypeReference | Type
As you can see, class is not a part of the field declaration, but of the type itself (TypeSpec can be used in other places where class or valuetype can be omitted). I am not 100 % sure why class or valuetype must be used in some cases, but there is also boxed TypeReference, so possibly originally, a form of the type could have been chosen as well.
User contributions licensed under CC BY-SA 3.0