7

I have some code that relied on methods not being inlined :

internal class MyClass : BaseClass
{
    // should not be inlined
    public void DoSomething(int id)
    {
       base.Execute(id);
    }
}

public abstract class BaseClass : MarshallByRefObject
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    protected void Execute(params object[] args)
    {
         // does a stack walk to find signature of calling method
    }
}

Of course this does only work if the DoSomething method is not inlined. This is the reason why the base class derives from MarshallByRefObject, which prevent inlining of public methods.

It worked until now, but I got a stacktrace from a .Net 4 server showing that the stackwalk reached the caller of DoSomething.

Is the .Net 4 inlining more clever and detect that MyClass is internal and has no chance of being substituted with a proxy ?

thinkbeforecoding
  • 6,668
  • 1
  • 29
  • 31
  • Can't work here. Only a call from outside of the class might have to be proxied. When the jitter is asked to generate code for the class itself then it *knows* that a local instance of the type needs it. So inlining is permitted. The changed inlining rules for .NET 4 is what actually bytes you. – Hans Passant Jan 19 '12 at 14:05
  • Actually, the DoSomething method is called from another class. That's why I think it should not be inlined... – thinkbeforecoding Jan 21 '12 at 19:16

1 Answers1

0

The commenter here suggests that you also need to specify NoOptimisation to achieve what you want.

http://connect.microsoft.com/VisualStudio/feedback/details/162364/methodimpl-methodimploptions-noinlining-doesnt-work-correctly-on-x64

Ben
  • 34,935
  • 6
  • 74
  • 113
  • The function missing on the stack is DoSomething. The Execute method is not inlined. But I should add the nooptimisation anyway on Execute to be sure it would never inline it. – thinkbeforecoding Jan 21 '12 at 19:18