Why is the C# compiler emitting a callvirt instruction for a GetType() method call?

前端 未结 5 1381
北荒
北荒 2020-11-30 05:40

I am curious to know why this is happening. Please read the code example below and the corresponding IL that was emitted in comments below each section:

usi         


        
5条回答
  •  悲哀的现实
    2020-11-30 06:14

    The compiler doesn't know the real type of o in the first expression, but it does know the real type in the second expression. It looks like it's only looking at one statement at a time.

    This is fine, because C# depends heavily on the JIT for optimization. It's very likely in such a simple case that both calls will become instance calls at runtime.

    I don't believe callvirt is ever emitted for non-virtual methods, but even if it was, it would be no problem because the method would never be overridden (for obvious reasons).

提交回复
热议问题