gpt4 book ai didi

c# - 在 .net 4.6.2 中使用 .net standard 1.5 lib 未命中 System.Runtime 4.1.0.0

转载 作者:太空狗 更新时间:2023-10-29 18:28:44 26 4
gpt4 key购买 nike

我在 .net 框架 4.6.2 控制台应用程序中使用 .net 标准时遇到一些问题。

我可以将问题简化为:鉴于:

我使用这个单一类创建了一个 .net 标准 1.5 客户端库 vs 2017

public class Class1
{
public List<int> Get()
{
return new List<int>() { 1, 2, 3, 4, 5, 65, 6 };
}
}

现在我创建一个新的 .net 4.6.2 控制台应用程序,它只是调用此类的方法:

       static void Main(string[] args)
{
var foo = new Class1();

Console.WriteLine("Done!");
Console.ReadLine();
}

现在我明白了

System.IO.FileNotFoundException: 'The File or Assembly "System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a was not found

当我将 .net standardlib nuget 包添加到 .net fx 控制台时,它可以正常工作。但是这样 system.runtime 就可以通过 GAC 和 nuget 引用实现,这对我来说似乎很丑陋。

我在这里推送了这个简短的测试解决方案:https://github.com/Gentlehag/NetStandardSample

我错过了什么?

最佳答案

我添加了一个 repo向您展示如何执行此操作。来自 README.md:

Requirements

Generally speaking, using libraries targeting .NET Standard in an application targeting .NET Framework requires the application project to include a NuGet reference for .NET Standard (NETStandard.Library). This ensures that the right set of assemblies are included with the application.

In Visual Studio 2015, the default way of consuming NuGet packages from .NET Framework projects is via packages.config. I don't recommend this path as this means that all assemblies are directly injected into the application project, which will significantly bloat your project file. Instead, I recommend you use project.json. To do this, perform the following steps:

  1. Uninstall all packages (if you're still using packages.config)
  2. Delete the empty packages.config
  3. Add project.json file with this content:

    json
    {
    "dependencies": {
    "NETStandard.Library": "1.6.0"
    },
    "runtimes": {
    "win": {}
    },
    "frameworks": {
    "net462": {}
    }
    }

Please note that you can generally depend on the latest version of the NETStandard.Library package, but you need to make sure to keep the framework moniker in sync with the version of .NET Framework your app is targeting, i.e. when you're targeting .NET Framework 4.6.1, you need to make sure to use net461 instead.

This feels clumsy

Yes it is. We're planning on addressing this in two ways:

  • We're replacing project.json with an MSBuild based solution in Visual Studio 2017. You'll still need to add the reference to NETStandard.Library, but you no longer have to mess with the way packages are being represented nor having to manually keep targeting information in sync.

  • We're planning to update .NET Framework so that future version of it come with built-in support for .NET Standard, in which case the reference will no longer be needed.

关于c# - 在 .net 4.6.2 中使用 .net standard 1.5 lib 未命中 System.Runtime 4.1.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com