当我尝试 运行 脚本时 Roslyn 崩溃

Roslyn crashes when I try to run a script

我有这个程序class:

using Microsoft.CodeAnalysis.CSharp.Scripting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoslynExperiment
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var result = ComputeAsync("1+2");
            Console.WriteLine(result.Result);
        }

        private static async Task<int> ComputeAsync(string formula)
        {
            return await CSharpScript.EvaluateAsync<int>(formula);
        }
    }
}

但是当我调用 ComputeAsync 时,出现此错误:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

Inner Exception
FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

为什么我在尝试执行我的脚本时会收到此异常?我该如何解决?

这是 Visual Studio 和 .Net 的记录问题。尝试将目标框架更改为 netstandard2.0

或者,您可以尝试将以下内容添加到您的 .csproj:

<ItemGroup>
  <PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
    <PrivateAssets>All</PrivateAssets>
  </PackageReference>
</ItemGroup>