我有这个程序课:
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.
为什么当我尝试执行脚本时会出现此异常?我该如何解决?
答案 0 :(得分:1)
这是Visual Studio和.Net的已记录问题。尝试将目标框架更改为netstandard2.0。
或者,您可以尝试将以下内容添加到.csproj中:
<ItemGroup>
<PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>