开发者

What is the difference between these CIL statements?

开发者 https://www.devze.com 2023-04-03 11:00 出处:网络
After running ILDASM on two assemblies generated from identical C# code (but potentially different compiler versions), I get the following output, from each respective dll.

After running ILDASM on two assemblies generated from identical C# code (but potentially different compiler versions), I get the following output, from each respective dll.

.locals init (class MyClass/'<>c__DisplayClass3' V_0)

and

.locals init ([0] class MyClass/'<>c__DisplayClass3' 'CS$<>8__locals4')

What is the differe开发者_如何学编程nce between these two statements, specifically, what is the significance of the '[0]' symbol?


The two statements are functionally identical. The difference is caused by ILDASM using data from a .pdb in the second case. When debugging information is available, ILDASM includes the variable name and index; otherwise it gives them generic names and omits the index.

I can only speculate that the index is included to help you associate any ldloc.x instructions with their variable names.

As an aside, explicitly supplying the variable index could be useful when editing MSIL by hand, since it may prevent you from making a mistake when adding or removing locals:

.locals init ([0] int32 x,
              [1] int32 y,
              [2] int32 z)
ldloc.1 //load y

If you later removed x without reviewing your method and fixing up your indexes:

.locals init ([1] int32 y, //error -- Undefined type of local var slot 0 in method frob
              [2] int32 z)
ldloc.1 //oops, would have loaded z


It is called a slot index. This index could be used to reference appropriate variable (the same as reference/access by name). In first example instanve would be accessed by V_0 name, in second by 0 index.

  • [0] - Slot Index
  • class MyClass/'<>c__DisplayClass3' - variable type
  • 'CS$<>8__locals4' - variable name

Do you have declared more than one variable of type MyClass along with line of code you've mentioned?

0

精彩评论

暂无评论...
验证码 换一张
取 消