IL: ldfld vs ldflda

I am writing a small IL-weaving application using Mono.Cecil that requires me to manipulate the target assembly at the IL level.

My question is pretty simple, but I still find this question very confusing. What is the practical difference between the ldfld and ldflda ?

I consulted with msdn and it seems that while ldfld selects the field value, ldflda gets the address of the field. Ok ... but what does that mean? At first I thought that the first is used for value types (and strings), and the second for reference types, but I compiled a C # fragment and checked it in Reflector, and this turned out to be wrong to me.

I was unable to find any single template when the C # compiler emits ldflda instead of ldfld , and my searches did not reveal any articles that could explain this. So when to use ldflda instead of ldfld ?

Any help would be greatly appreciated.

+7
source share
2 answers

I assume it is used for struct ures:

 struct Counter { public int i; } struct Something { public Counter c; } Something s; sci++; 

I am sure that c is loading here at the address, otherwise it will create a copy of Counter . That is why you cannot handle it.

+7
source

If you pass the field to the subroutine as the ref parameter, you should see that the compiler emits ldflda for this to get a link to go through.

+1
source

All Articles