NetStandard 和 .Net Framework 之间的引用 [英] Referencing between NetStandard and .Net Framework

查看:87
本文介绍了NetStandard 和 .Net Framework 之间的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 .Net Framework 和 NetStandard 程序集相互通信(以了解可能的情况).我目前有四个项目,两个 Framework 4.5.2 项目和两个 NetStandard1.2 项目:

  1. Framework452.Library
  2. NetStandard12.CentralLibrary
  3. NetStandard12.BaseLibrary
  4. Framework452.Tests

引用结构为:

  • Framework452.Tests 引用 NetStandard12.CentralLibrary:通过将 NetStandard.Library nuget 包添加到 Framework452.Tests 来工作.
  • NetStandard12.CentralLibrary 参考 NetStandard12.BaseLibrary:无需修改即可工作.
  • NetStandard12.CentralLibrary 引用 Framework452.Library:不工作,即使 Framework452.Library 安装了 NetStandard.Library nuget 包.

NetStandard 项目可以引用框架项目吗?如果是这样,我需要做什么才能让他们进行交流?目前我可以添加引用,但它对代码不可见.

更新

我重新创建了解决方案并添加了下面的代码,当我尝试编译时,Framework452.Tests 项目出现以下错误:

<块引用>

错误 CS0006:元数据文件'~TryNETStandardNetStandard12.CentralLibraryinDebug etstandard1.2NetStandard12.CentralLibrary.dll'找不到.

命名空间 Framework452.Library{公共类 Returner452 {public static bool ReturnTrue() { return true;}}}使用 Xunit;命名空间 Framework452.Tests{公共类 Class1 {[事实]公共无效框架测试(){Assert.True(NetStandard12.CentralLibrary.Class1.Return452());}[事实]公共无效 NetStandardTest() {Assert.True(NetStandard12.CentralLibrary.Class1.Return12());}}}命名空间 NetStandard12.BaseLibrary{公共类 Returner12 {public static bool ReturnTrue() { return true;}}}使用 Framework452.Library;使用 NetStandard12.BaseLibrary;命名空间 NetStandard12.CentralLibrary{公开课 Class1{public static bool Return452() { return Returner452.ReturnTrue();}public static bool Return12() { return Returner12.ReturnTrue();}}}

解决方案

根据本页

这是NetStandard12.CentralLibrary中的Class1.cs

代码编译正常,没有任何错误.

注意:如果 Framework452.Library 使用 .NET Standard 1.2 不支持的 API(例如 Winforms、Win32 API 或任何 Microsoft 专有的 API),您的代码可能会失败对跨平台没有意义的库).

我在 MSFT 根据图表,依赖 .NET Standard 的库无法看到/使用 .NET 框架实现.

当 .NET Standard 2 发布时,这可能会有所改变,您可以通过 Compatibility Shim 引用 .NET Framework.观看此视频以获得更深入的解释 https://youtu.be/vg6nR7hS2lI?list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY&t=169

I'm trying to get .Net Framework and NetStandard assemblies to communicate with each other (to learn what is possible). I currently have four projects, two Framework 4.5.2 projects and two NetStandard1.2 projects:

  1. Framework452.Library
  2. NetStandard12.CentralLibrary
  3. NetStandard12.BaseLibrary
  4. Framework452.Tests

The referencing structure is:

  • Framework452.Tests references NetStandard12.CentralLibrary: working by adding the NetStandard.Library nuget package to Framework452.Tests.
  • NetStandard12.CentralLibrary references NetStandard12.BaseLibrary: working without modification.
  • NetStandard12.CentralLibrary references Framework452.Library: Not working, even when Framework452.Library has the NetStandard.Library nuget package installed.

Can NetStandard projects reference Framework projects? If so, what do I need to do to get them to communicate? At the moment I can add the reference, but it is not visible to the code.

Update

I recreated the solution and added the code below, which when I try to compile gives the following error from the Framework452.Tests project:

Error CS0006: Metadata file '~TryNETStandardNetStandard12.CentralLibraryinDebug etstandard1.2NetStandard12.CentralLibrary.dll' could not be found.

namespace Framework452.Library
{
    public class Returner452 {
        public static bool ReturnTrue() { return true; }
    }
}


using Xunit;
namespace Framework452.Tests
{
    public class Class1 {
        [Fact]
        public void FrameworkTest() {
            Assert.True(NetStandard12.CentralLibrary.Class1.Return452());
        }

        [Fact]
        public void NetStandardTest() {
            Assert.True(NetStandard12.CentralLibrary.Class1.Return12());
        }
    }
}


namespace NetStandard12.BaseLibrary
{
    public class Returner12 {
        public static bool ReturnTrue() { return true; }
    }
}


using Framework452.Library;
using NetStandard12.BaseLibrary;
namespace NetStandard12.CentralLibrary
{
    public class Class1
    {
        public static bool Return452() { return Returner452.ReturnTrue(); }
        public static bool Return12() { return Returner12.ReturnTrue(); }
    }
}

解决方案

According to this page https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-platforms-support you should be able to achieve your purpose because .NET Standard 1.2 support .NET Framework 4.5.1 (UPDATE: This statement is not 100% correct. Please see the Update section below.)

I tried to set up a solution in VS 2017 and set the references as you described. Here is the result.

and this is the Class1.cs in NetStandard12.CentralLibrary

The code compiles fine without any errors.

Note: your code may fail if the Framework452.Library uses an API that is not supported by .NET Standard 1.2 (e.g Winforms, Win32 API or any Microsoft proprietary library that does not make sense for cross platform).

I recommend this youtube playlist on the .NET standard introduction from one of the MSFT https://www.youtube.com/watch?v=YI4MurjfMn8&list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY

In .NET Standard - Checking Compatibilty , he recommended tools to help you find out what API is not supported in the .NET Standard.

Thing will become easier with .NET Standard 2.0 and 'compat shim'

UPDATE:

After trying again with more data provided by the question, it's true that a library targeting (depends) .NET Standard could not depend on a library that target .NET Framework. For some strange reason, the compiler allows me to compile the example that I gave above. This could be a bug in tooling.

After a little more research, I found a good example demonstrate the relationship between NetStandard and NetFramework: How .NET Standard relates to .NET Platform. The graph here show the dependencies According to the graph, there is no way a library that depends on .NET Standard could see/use the .NET framework implementation.

When .NET Standard 2 is released, this may change a little bit and you could reference .NET Framework via Compatibility Shim. See this video for more in-depth explanation https://youtu.be/vg6nR7hS2lI?list=PLRAdsfhKI4OWx321A_pr-7HhRNk7wOLLY&t=169

这篇关于NetStandard 和 .Net Framework 之间的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆