从 Windows 类库引用 .NET Standard 库 [英] Referencing a .NET Standard library from a Windows Class Library

查看:22
本文介绍了从 Windows 类库引用 .NET Standard 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中目前有两个项目:一个 Windows 类库(面向 .NET Framework 4.6.1) 和另一个面向 .NET Standard 1.3 的类库.我使用的是 Visual Studio 2015 更新 3.

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.

我从另一个项目中添加了对 .NET Standard 项目的引用,它出现在引用列表中,但是当我想使用它们时,我看不到引用库中的任何类或命名空间(即使引用的库已成功构建并且没有错误).

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).

这是 .NET Standard 库项目的 project.json:

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"
    }
  }
}

我以为.NET 4.6.1项目可以使用.NET Standard 1.3 libs,我什至尝试使用较低版本(1.0),但结果是一样的.我在这里错过了什么?

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?

如果我跑

dotnet 恢复

它也能正常工作:

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.

推荐答案

从 Windows 类库中引用 .NET Core 项目应该是可能的.但是——.NET Standard 库与 .NET Framework 的早期版本(即 4.6.1 或更低版本)不直接兼容"..NET Standard 库是一个,其中包含已经存在在 .NET Framework(例如 4.6.1)中的组件.不同之处在于 .NET Standard 库是为跨平台的 .NET Standard 框架构建的.

Referencing a .NET Core project from a Windows Class Library should be possible. However—the .NET Standard library is not "directly compatible" with previous versions of .NET Framework, i.e., 4.6.1 or below. The .NET Standard library is a package with components that already exist within the .NET Framework (4.6.1 for instance). The difference is that the .NET Standard library is built for the cross-platform .NET Standard framework.

您可以在 project.json 文件的 "frameworks" 部分下定位多个框架.

You may, target multiple frameworks under the "frameworks" section in your project.json-file.

同时,您还应该将 "NETStandard.Library"-依赖项直接移至 "netstandard1.x"-框架下.

While doing so, you should also move the "NETStandard.Library"-dependency directly under the "netstandard1.x"-framework.

示例 project.json

{
  "version": "1.0.0-*",

  "dependencies": { },

  "frameworks": {
    "net461": { },
    "netstandard1.3": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      },
      "imports": "dnxcore50"
    }
  }
}

这可确保您不会对 NET Standard 库包含任何多余的依赖项,因为这些依赖项仅在针对 NET Standard 框架构建时才会被包含在内.如果针对 .NET Framework 4.6.1 构建,则忽略这些依赖项.这很好——因为这些依赖项已经是 .NET Framework 的一部分(如上所述).

This ensures that you do not include any superfluous dependencies towards the NET Standard library as these dependencies will only get included when building towards the NET Standard framework. If built against .NET Framework 4.6.1, these dependencies are omitted. This is fine—as these dependencies are already part of the .NET Framework (as described above).

现在假设您要引用的内容不是 .NETStandard 库的一部分,而是 .NET 4.6.1 框架的一部分.在我的工作场所,一个常见的例子是 System.ComponentModel.DataAnnotations.它是 .NET Framework 的一部分,但它是 .NET Standard 框架的单独包.

Now say for instance you want to reference something that is not part of the .NETStandard library, but part of the .NET 4.6.1 framework. A common case for this at my workplace is System.ComponentModel.DataAnnotations. It is part of the .NET Framework, but a separate package for the .NET Standard framework.

然后您必须将其作为"net461"框架程序集引用,但作为"的依赖项引用netstandard1.x" 框架.

You will then have to reference it as a framework assembly for "net461", but as a dependency for the "netstandard1.x" framework.

示例 project.json

"frameworks": {
  "net461": {
    "frameworkAssemblies": {
      "System.ComponentModel.DataAnnotations": "4.0.0.0"
    }
  },
  "netstandard1.3": {
    "dependencies": {
      "NETStandard.Library": "1.6.0",
      "System.ComponentModel.Annotations": "4.1.0"
    },
    "imports": "dnxcore50"
  }
}

<小时>

正如@meziantou 所描述的:


As @meziantou describes:

在面向完整框架的项目中引用 .NET Standard 尚无法正常工作.

Referencing .NET Standard in a project that targets the full framework does not work correctly yet.

我刚刚在 Visual Studio 2015 中对其进行了测试,我可以确认—引用已添加,但您不能使用引用库的任何组件.

I just tested it in Visual Studio 2015, and I can confirm—the reference gets added, but you cannot use any component of the referenced library.

如果您没有安装 Visual Studio 2017,我能想到的唯一解决方案是 dotnet pack 并将其发布到 NuGet-feed.为此,您可以设置本地 NuGet 提要.

If you don't have Visual Studio 2017 installed, the only solution I can think of is to dotnet pack your project and publish it to a NuGet-feed. You may set up a local NuGet feed for this purpose.

然后只需在 NuGet 包管理器控制台中使用 Install-Package cmdlet.

Then simply use the Install-Package cmdlet in the NuGet package manager console.

Install-Package <your-package> -v 1.0.0-<x>

包管理器将引用包的正确版本 (.NET 4.6.1).

The package-manager will reference the correct version of the package (.NET 4.6.1).

这篇关于从 Windows 类库引用 .NET Standard 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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