We are using Expression.Lambda to compile Delegates.
Recently we noticed that the "toplevel" method that gets baked into the target Delegate is missing in the stacktrace when an exception is thrown.
Full Example to reproduce this behaviour:
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace Sandbox
{
public class Program
{
private static void Main()
{
var methodInfo = typeof(X).GetMethod(nameof(X.Method1), BindingFlags.Static | BindingFlags.Public);
var call = Expression.Call(methodInfo);
var compiledDelegate = Expression.Lambda<Action>(call, null).Compile();
try
{
compiledDelegate();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.ReadKey();
}
public class X
{
public static void Method1()
{
Method2();
}
public static void Method2()
{
Console.WriteLine("Strange");
throw new Exception();
}
}
}
}
You will get the following StackTrace when running the .exe (releasebuild)
System.Exception: Eine Ausnahme vom Typ "System.Exception" wurde ausgelöst.
bei Sandbox.Program.X.Method2() in [..]\Program.cs:Zeile 38.
bei lambda_method(Closure )
bei Sandbox.Program.Main() in [..]\Program.cs:Zeile 18.
Notice that Method1 is missing.
My Question here is: How can I make Method1 appear and why isnt it appearing?
Method1 seems to get inlined but that shouldnt remove it from the callstack or am I wrong?