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

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

问题描述

我的解决方案中目前有两个项目:一个 Windows类库(针对.NET Framework 4.6.1)和另一个针对 .NET Standard 1.3的类库.我正在使用Visual Studio 2015 Update 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库,我什至尝试使用较低版本(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:\Users\Zsolt\Documents\Visual Studio 2015\Projects\PWB\PWBSpreadsheet.Entities\project.json...
log  : Restoring packages for C:\Users\Zsolt\Documents\Visual Studio 2015\Projects\PWB\PWBSpreadsheet.Parser\project.json...
log  : Writing lock file to disk. Path: C:\Users\Zsolt\Documents\Visual Studio 2015\Projects\PWB\PWBSpreadsheet.Parser\project.lock.json
log  : C:\Users\Zsolt\Documents\Visual Studio 2015\Projects\PWB\PWBSpreadsheet.Parser\PWBSpreadsheet.Parser.xproj
log  : Restore completed in 408ms.
log  : Writing lock file to disk. Path: C:\Users\Zsolt\Documents\Visual Studio 2015\Projects\PWB\PWBSpreadsheet.Entities\project.lock.json
log  : C:\Users\Zsolt\Documents\Visual Studio 2015\Projects\PWB\PWBSpreadsheet.Entities\PWBSpreadsheet.Entities.xproj
log  : Restore completed in 417ms.

推荐答案

应该从Windows类库中引用.NET Core项目 .但是,.NET标准库与.NET Framework的早期版本(即4.6.1或更低版本)不直接兼容". .NET标准库是一个程序包,其中包含.NET框架中的已经存在的组件(例如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" -dependency直接移到"netstandard1.x" -framework下.

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.

然后,您必须将其作为框架的框架程序集引用,而对于"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,我能想到的唯一解决方案是本地NuGet Feed .

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标准库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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