Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

c# - Referencing a .NET Standard library from a Windows Class Library

There are two projects in my solution currently: a Windows Class Library (targeting .NET Framework 4.6.1) and another class library that targets .NET Standard 1.3. I'm using Visual Studio 2015 Update 3.

I've added a reference to the .NET Standard project from the other project and it appears in the list of references, but I can't see any of the classes or namespaces from the referenced library when I want to use them (even though the referenced library was successfully built and has no errors).

This is the project.json for the .NET Standard library project:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0"
  },

  "frameworks": {
    "netstandard1.3": {
      "imports": "dnxcore50"
    }
  }
}

I thought that .NET 4.6.1 projects can use .NET Standard 1.3 libs, and I even tried to use lower versions (1.0), but the result is the same. What am I missing here?

If I run

dotnet restore

it also works fine:

log  : Restoring packages for C:UserssoltDocumentsVisual Studio 2015ProjectsPWBPWBSpreadsheet.Entitiesproject.json...
log  : Restoring packages for C:UserssoltDocumentsVisual Studio 2015ProjectsPWBPWBSpreadsheet.Parserproject.json...
log  : Writing lock file to disk. Path: C:UserssoltDocumentsVisual Studio 2015ProjectsPWBPWBSpreadsheet.Parserproject.lock.json
log  : C:UserssoltDocumentsVisual Studio 2015ProjectsPWBPWBSpreadsheet.ParserPWBSpreadsheet.Parser.xproj
log  : Restore completed in 408ms.
log  : Writing lock file to disk. Path: C:UserssoltDocumentsVisual Studio 2015ProjectsPWBPWBSpreadsheet.Entitiesproject.lock.json
log  : C:UserssoltDocumentsVisual Studio 2015ProjectsPWBPWBSpreadsheet.EntitiesPWBSpreadsheet.Entities.xproj
log  : Restore completed in 417ms.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Referencing .NET Standard in a project that targets the full framework does not work correctly yet. Instead, your project must target multiple frameworks.

If you are using the new tooling (with VS 2017), i.e. the csproj project format, you can set multiple target in the TargetFrameworks tag:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard1.3;net46</TargetFrameworks>
  </PropertyGroup>
</Project>

If you need to, you can set different dependencies for each target:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netstandard1.3;net46</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
    <PackageReference Include="Microsoft.Win32.Primitives" Version="4.3.0" />
    <PackageReference Include="System.Collections" Version="4.3.0" />
    <PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net46'">
    <PackageReference Remove="NETStandard.Library" />
  </ItemGroup>
</Project>

By default the NETStandard.Library is automatically added. If you want to remove it, use PackageReference Remove="NETStandard.Library"/>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...