Referencing between NetStandard and .Net Framework

人盡茶涼 提交于 2020-06-10 19:08:10

问题


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 '~\TryNETStandard\NetStandard12.CentralLibrary\bin\Debug\netstandard1.2\NetStandard12.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(); }
    }
}

回答1:


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




回答2:


No, .NET Standard projects cannot reference framework projects.

.NET Standard projects need to be usable across platforms, forcing a dependency on the .NET framework by referencing an assembly targeting it makes this impossible.

Note that with some of the magic Microsoft is doing with .NET Standard 2.0 this is less true but the overall idea still stands.



来源:https://stackoverflow.com/questions/45291737/referencing-between-netstandard-and-net-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!