如何使用 ILGenerator 设置 .maxstack 指令?
例如,典型的 setter 方法的 .maxstack 为 2:
.maxstack 2 // The evaluation stack has a max size of 2
IL_0000: ldarg.0 // the current instance (this)
IL_0001: ldarg.1 // new value
IL_0002: stfld Int32 _someField // stores the new value on _someField
IL_0007: ret // Return to caller
可以使用 ILGenerator
设置元数据,因为它实际上提供 DefineLabel和 DeclareLocal ,那么为什么它不提供 SetMaxStack(short/int)
方法或类似的方法呢?
如果我无法设置此元数据,Reflecion.Emit
如何确定堆栈的大小?是否将其设置为最大可能值?或者堆栈可能在运行时自动扩展?
最佳答案
ILGenerator
类将跟踪堆栈大小。看看reference source 。例如UpdateStackSize涉及到函数。
关于c# - 如何使用 ILGenerator 设置 ".maxstack",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656409/