• Roslyn还出现这么低级的错误,不应该呀!


    前几天对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方法输出的结果可以正常出现在入下所示的控制台上。

    image

    接下来我们将“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:usersjinnanSourceReposAppAppinDebug etcoreapp2.1App.dll]”。

    image

    如下所示的是完整的错误信息:

    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:usersjinnanSourceReposAppAppinDebug
    etcoreapp2.1App.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:usersjinnanSourceReposAppAppProgram.cs:line 31
       at Scripting.Program.<Main>(String[] args)

    这是我提交的issue链接:https://github.com/dotnet/roslyn/issues/28803#issuecomment-407894956

  • 相关阅读:
    jquery 备忘笔记
    spring 集成 mybatis 后数据源初始化失败问题分析
    SpringMVC 资源国际化实现以及常见问题
    IE浏览器 get请求缓存问题
    Comparison method violates its general contract! 异常原因
    Security基础(六):部署Zabbix监控平台、配置及使用Zabbix监控系统、自定义Zabbix监控项目、实现Zabbix报警功能
    LVS负载均衡中arp_ignore和arp_annonuce参数配置
    iptables防火墙相关命令详解
    <三剑客> 老大:awk命令用法
    <三剑客> 老三:grep命令用法
  • 原文地址:https://www.cnblogs.com/artech/p/roslyn-error.html
Copyright © 2020-2023  润新知