这可能是一个愚蠢的问题,但我真的很好奇我是否能做到这一点。我编写了以下示例程序:
class Test1 {
public const int b = 8;
public static int z = 3;
public static void Main(string[] args){
const int q = 6;
Console.WriteLine(q);
Console.WriteLine(b);
Console.WriteLine(z);
}
}
Y 编译了这个mcs
,然后我用monodis
. 得到以下代码:
.field public static literal int32 b = int32(0x00000008)
.field public static int32 z
// method line 2
.method public static hidebysig
default void Main (string[] args) cil managed
{
// Method begins at RVA 0x2058
.entrypoint
// Code size 23 (0x17)
.maxstack 8
IL_0000: ldc.i4.6
IL_0001: call void class [mscorlib]System.Console::WriteLine(int32)
IL_0006: ldc.i4.8
IL_0007: call void class [mscorlib]System.Console::WriteLine(int32)
IL_000c: ldsfld int32 Test.Test1::z
IL_0011: call void class [mscorlib]System.Console::WriteLine(int32)
IL_0016: ret
} // end of method Test1::Main
我想用来ldsfld
加载b的值。如果我将 IL_0006 更改为ldsfld int32 Test.Test1::b
,它将组装,但是当我运行可执行文件时,我得到一个异常:
Unhandled Exception:
System.InvalidProgramException: Invalid IL code in Test.Test1:Main
(string[]): IL_0006: ldsfld 0x04000001
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException:
Invalid IL code in Test.Test1:Main (string[]): IL_0006: ldsfld 0x04000001
有什么方法可以使用static literal
而不是仅仅使用ldc
?