首页 > 解决方案 > 使用 .NET Core 3.1 运行 C# 脚本时出现 FileLoadException

问题描述

我编写了以下 C# 脚本(HelloWorld.csx 文件):

#! "netcoreapp3.1"
#r "nuget: System.Text.Encoding.CodePages, 5.0.0"

public class Script
{
    public static void Run()
    {
        try
        {
            System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex.ToString());
        }
    }
}

Script.Run();

我正在使用dotnet-script(1.0.1 版)和.NET 运行时(3.1.14 版)

执行此脚本时,出现以下错误。知道为什么吗?

dotnet-script HelloWorld.csx

System.IO.FileLoadException:无法加载文件或程序集“System.Text.Encoding.CodePages,版本=5.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”。无法找到或加载特定文件。(0x80131621) 文件名:'System.Text.Encoding.CodePages,版本=5.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'---> System.IO.FileLoadException:无法加载文件或程序集'System.Text。 Encoding.CodePages,版本=5.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'。在 System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly) 在 System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath) 在 System.Reflection.Assembly.LoadFrom(String assemblyFile) 在System.Reflection.Assembly。

标签: c#.net.net-corecsharpscriptdotnet-script

解决方案


我在dotnet-script repo上创建了一个问题,并从开发人员那里得到了答案。有关所有详细信息,请参阅问题

我得到的答案是 dotnet-script 1.0.1 版预加载了 System.Text.Encoding.CodePages 的 4.7.1 版,这意味着无法解决较新的专业。


推荐阅读