前几天对Dora.Interception作了简单的重构,想提供C#脚本来定义Interception Policy,毫无疑问微软提供的编译平台Roslyn使C#脚本化提供了支持。但是没有想到随便尝试了一个简单的功能就出现了问题,我个人觉得这应该是Roslyn的Bug。但是Roslyn经历了这么多次版本的迭代还出现如此低级的错误,实在有点说不过去。
闲话少说,我们现在通过几行简单的代码来重现这个Bug。我们创建了一个.NET Core 2.1的控制台应用,并添加针对NuGet包”Microsoft.CodeAnalysis.CSharp.Scripting“(版本2.8.2)的依赖。如下所示的是.csproj的完整定义。
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.1</TargetFramework> <AssemblyName>App</AssemblyName> <RootNamespace>App</RootNamespace> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <LangVersion>7.3</LangVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="2.8.2" /> </ItemGroup></Project>
接下来我们编写了一段简单的程序。如下面的代码片段所示,我们只是采用脚本的形式创建了一个Foo对象并利用它创建出Bar对象,然后调用后者的Print方法而已。
namespace Scripting { public class Foo { } public class Bar { public Foo Foo { get; } public Bar(Foo foo) => Foo = foo; public void Print() => Console.WriteLine(Foo); } class Program { static async Task Main(string[] args) { var options = ScriptOptions.Default .AddReferences(typeof(Program).Assembly) .AddImports("Scripting"); //Success await CSharpScript .Create("var bar = new Bar(new Foo());", options) .ContinueWith("bar.Print()") .RunAsync(); } } }
这段代码是没有问题的,运行之后Print方法输出的结果可以正常出现在入下所示的控制台上。
接下来我们将“var bar = new Bar(new Foo());”这行代码按照如下的方式拆分成两行执行。
class Program { static async Task Main(string[] args) { //Fail await CSharpScript .Create("var foo = new Foo();", options) .ContinueWith("var bar = new Bar(foo);") .ContinueWith("bar.Print()") .RunAsync(); } }
在此运行该程序后出现如下所示的“Microsoft.CodeAnalysis.Scripting.CompilationErrorException”异常,并提示“cannot convert from 'Scripting.Foo [App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]' to 'Scripting.Foo [c:\users\jinnan\Source\Repos\App\App\bin\Debug\netcoreapp2.1\App.dll]”。
如下所示的是完整的错误信息:
Unhandled Exception: Microsoft.CodeAnalysis.Scripting.CompilationErrorException: (1,19): error CS1503: Argument 1: cannot convert from 'Scripting.Foo [App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]' to 'Scripting.Foo [c:\users\jinnan\Source\Repos\App\App\bin\Debug\netcoreapp2.1\App.dll]' at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.ThrowIfAnyCompilationErrors(DiagnosticBag diagnostics, DiagnosticFormatter formatter) in /_/src/Scripting/Core/ScriptBuilder.cs:line 104 at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.CreateExecutor[T](ScriptCompiler compiler, Compilation compilation, Boolean emitDebugInformation, CancellationToken cancellationToken) in /_/src/Scripting/Core/ScriptBuilder.cs:line 89 at Microsoft.CodeAnalysis.Scripting.Script`1.GetExecutor(CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 359 at Microsoft.CodeAnalysis.Scripting.Script`1.CommonGetExecutor(CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 343 at Microsoft.CodeAnalysis.Scripting.Script`1.TryGetPrecedingExecutors(Script lastExecutedScriptInChainOpt, CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 407 at Microsoft.CodeAnalysis.Scripting.Script`1.GetPrecedingExecutors(CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 370 at Microsoft.CodeAnalysis.Scripting.Script`1.RunAsync(Object globals, Func`2 catchException, CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 459 at Scripting.Program.Main(String[] args) in c:\users\jinnan\Source\Repos\App\App\Program.cs:line 31 at Scripting.Program.<Main>(String[] args)
这是我提交的issue链接:https://github.com/dotnet/roslyn/issues/28803#issuecomment-407894956
作者:蒋金楠
微信公众账号:大内老A
微博:www.weibo.com/artech
如果你想及时得到个人撰写文章以及著作的消息推送,或者想看看个人推荐的技术资料,可以扫描左边二维码(或者长按识别二维码)关注个人公众号)。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。