Roslyn Scripts: Line Number Information for Runtime Exceptions

I was messing around with Roslyn script files (using the Microsoft.CodeAnalysis.CSharp.Scriptingnuget package ), and I am wondering if there is a way to add line number information to stack traces for exceptions that occur inside the script.

When I run the following C # code:

// using Microsoft.CodeAnalysis.CSharp.Scripting;

var code = @"
var a = 0;
var b = 1 / a;
";
try
{
    await CSharpScript.RunAsync(code);
}
catch (DivideByZeroException dbze)
{
    Console.WriteLine(dbze.StackTrace);
}

Stack trace written to the console:

   at Submission#0.<<Initialize>>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.<RunSubmissionsAsync>d__9`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.Scripting.Script`1.<RunSubmissionsAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at UnitTests.ExploreRoslyn.<ScriptWithRuntimeError>d__4.MoveNext() in D:\dev\misc\netmockery\UnitTests\ExploreRoslyn.cs:line 47

Note that if I try to catch an exception inside the script, the result will be similar:

var code = @"
try  {
    var a = 0;
    var b = 1 / a;
}
catch (System.DivideByZeroException dbze)
{
    Console.WriteLine(dbze.StackTrace);
}
";
await CSharpScript.RunAsync(code);

It is output:

at Submission#0.<<Initialize>>d__0.MoveNext()

To the console.

Is there a way to get the Roslyn script generator to add debugging information when compiling / executing the script, so I can get the line number information in the stack trace?

+1
1

- , ( ) .

:

var code = @"
var a = 0;
var b = 1 / a;
";

var script = CSharpScript.Create(code);
var compilation = script.GetCompilation();
var ilstream = new MemoryStream();
var pdbstream = new MemoryStream();
compilation.Emit(ilstream, pdbstream);

var assembly = Assembly.Load(ilstream.GetBuffer(), pdbstream.GetBuffer());
var type = assembly.GetType("Submission#0");
var factory = type.GetMethod("<Factory>");
var submissionArray = new object[2];
Task<object> task = (Task<object>)factory.Invoke(null, new object[] { submissionArray });

try
{
    await task;
}
catch (DivideByZeroException dbze)
{
    Console.WriteLine(dbze.StackTrace);
}

( :line 3 ):

   at Submission#0.<<Initialize>>d__0.MoveNext() in :line 3
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at UnitTests.ExploreRoslyn.<ExploreEmittingAssembly>d__13.MoveNext() in D:\dev\misc\netmockery\UnitTests\ExploreRoslyn.cs:line 151

, , , script (Submission#0, <Factory>), , . (, , ?) .

Update

https://github.com/dotnet/roslyn/issues/13482 Roslyn.

+1

Source: https://habr.com/ru/post/1654458/


All Articles