Is there a way to insert debug info into .NET expression tree, so that it's shown in the exception trace (e.g., source code location and custom function name).
For example, given the code (that defines a function that divides its two arguments, and calls it with second parameter being 0
):
// Define the lambda expression
var x = Expression.Parameter(typeof(int), "x");
var y = Expression.Parameter(typeof(int), "y");
var op = Expression.Divide(x, y);
var debug = Expression.DebugInfo(
Expression.SymbolDocument("test.txt"),
1, 5, 4, 13);
var bl = Expression.Block(debug, op);
var lambdaExpr = Expression.Lambda<Func<int, int, int>>(
bl,
new[] { x, y });
// Compile the lambda expression to a delegate
var compiled = lambdaExpr.Compile();
// Create a function that invokes the compiled delegate with some arguments
Func<int, int, int> invokeDelegate = (xArg, yArg) => compiled(xArg, yArg);
// Invoke the function with some arguments
invokeDelegate(20, 0);
The resulting stack trace ends up being:
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.
at lambda_method1(Closure , Int32 , Int32 )
at ExpressionTreeExample.<>c__DisplayClass0_0.<Main>b__0(Int32 xArg, Int32 yArg) in .../workspace/test/Program.cs:line 23
at ExpressionTreeExample.Main() in .../workspace/test/Program.cs:line 26
In this example, is there a way to add debug information to the expression so that "test.txt" (and other provided information) is shown in the exception trace?
Note: I'm interested in .NET Core, not Framework. Thanks!