如何使我的托管 NuGet 包支持 C++/CLI 项目?

How can I make my managed NuGet package support C++/CLI projects?(如何使我的托管 NuGet 包支持 C++/CLI 项目?)
本文介绍了如何使我的托管 NuGet 包支持 C++/CLI 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 NuGet 包,当我在 C# 项目中使用它时效果很好.它在 lib/net40 目录中包含一个 DLL,该 DLL 被添加为引用.

既然 NuGet 支持 C++,我如何实际修改我的包,以便可以将 DLL 作为托管引用添加到 C++/CLI 项目中?我找不到任何解释这一点的教程.如果我尝试按原样添加包,则会收到以下错误:

<块引用>

您正在尝试将此包安装到以Native,Version=v0.0"为目标的项目中,但该包不包含任何与该框架兼容的程序集引用或内容文件.

有人会认为解决方案是将文件放在 lib/native 下,但根据 http://docs.nuget.org/docs/reference/support-for-native-projects,不支持.此外,简单地将 DLL 直接放在 lib 下似乎没有任何作用.

显然,我应该使用 build/native 下的 .props.targets 文件来执行此操作,但是我需要将什么放入这些文件中才能制作这份工作?

解决方案

正如 Patrick O'Hara 所写,NuGet 将不要为您更改 C++/CLI 项目.请参阅 GitHub 问题 NuGet/Home#1121 - 无法将托管包安装到 CLI 项目中.但是,使用 NuGet 命令行实用程序 NuGet.exe,您可以让 NuGet 下载并解压缩所需的包.

对于一个完整的示例,我采取了以下步骤来添加对 OptimizedPriorityQueue 的引用Visual Studio 2013 C++/CLI 项目中的 1.0.0:

  1. 如果尚未打开包管理器控制台,请打开(工具 > NuGet 包管理器 > 包管理器控制台).
  2. 在包管理器控制台中,安装 NuGet.CommandLine 包:

    <上一页>安装包 NuGet.CommandLine

    (注意:在撰写本文时,NuGet.CommandLine 的最新版本是 2.8.6.您可能会有所不同.)

  3. 在您的项目文件夹中,现在应该有一个 .nugetpackages.config XML 文件,其中包含以下内容:

    <?xml version="1.0" encoding="utf-8"?><包><package id="NuGet.CommandLine" 版本="2.8.6"/></包>

  4. 在 Notepad++ 等文本编辑器中,为所需的包添加 元素.在这种情况下,我补充说:

    <package id="OptimizedPriorityQueue" version="1.0.0"/>

    .. 在 <packages> 元素中.

  5. 打开一个命令提示符(我打开了一个 VS2013 开发者命令提示符,但是一个常规的命令提示符应该可以工作.)

  6. cd 进入项目文件夹.
  7. 运行以下命令,如果不同,则更改 NuGet.CommandLine 的版本号:

    <上一页>.packagesNuGet.CommandLine.2.8.6 oolsNuGet.exe 安装 -NonInteractive -OutputDirectory 包 .nugetpackages.config

    对我来说,输出是:

    <上一页>安装OptimizedPriorityQueue 1.0.0.0".成功安装OptimizedPriorityQueue 1.0.0.0".packages.config 中列出的所有软件包都已安装.

  8. 在 Visual Studio 中右键单击项目并选择属性.在 Common Properties > References 下,点击 Add New Reference… 按钮.
  9. 选择左侧的浏览.在 Add Reference 对话框的 OK 和 Cancel 按钮旁边,有一个 Browse… 按钮.点击打开文件选择对话框.
  10. 导航到 NuGet 解压到项目文件夹的 packages 子目录的 DLL,然后单击 Add 按钮.单击确定关闭添加引用"对话框.
  11. 您现在应该可以在 C++/CLI 项目中使用该程序集了:

    使用命名空间Priority_Queue;//...

I have made a NuGet package that works well when I use it from a C# project. It contains a DLL in the lib/net40 directory, and the DLL gets added as a reference.

Now that NuGet supports C++, how do I actually modify my package so that the DLL can be added as a managed reference in a C++/CLI project? I can't find any tutorials explaining this. If I try to just add the package as is, I get the following error:

You are trying to install this package into a project that targets 'Native,Version=v0.0', but the package does not contain any assembly references or content files that are compatible with that framework.

One would think that the solution is to put the files under lib/native, but according to http://docs.nuget.org/docs/reference/support-for-native-projects, that is not supported. Also, simply putting the DLL directly under lib doesn't seem to do anything.

Apparently, I am supposed to do this with a .props or .targets file under build/native, but what do I need to put into those files to make this work ?

解决方案

As Patrick O'Hara wrote, NuGet will not make changes to a C++/CLI project for you. See GitHub Issue NuGet/Home#1121 - Cannot install managed packages into a CLI project. However, using the NuGet command line utility, NuGet.exe, you can have NuGet download and unpack the desired package(s).

For a complete example, here were steps that I took to add a reference to OptimizedPriorityQueue 1.0.0 in a Visual Studio 2013 C++/CLI project:

  1. Open the Package Manager Console if not already open (TOOLS > NuGet Package Manager > Package Manager Console).
  2. In the Package Manager Console, install the NuGet.CommandLine package:

    Install-Package NuGet.CommandLine
    

    (Note: As of this writing, the latest version of NuGet.CommandLine is 2.8.6. It may be different for you.)

  3. Within your project folder, there should now be a .nugetpackages.config XML file with the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="NuGet.CommandLine" version="2.8.6" />
    </packages>
    

  4. In a text editor such as Notepad++, add a <package> element for the desired package. In this case, I added:

    <package id="OptimizedPriorityQueue" version="1.0.0" />
    

    .. within the <packages> element.

  5. Open a command prompt (I opened a VS2013 Developer Command Prompt, but a regular command prompt should work.)

  6. cd into the project folder.
  7. Run the following command, changing the version number of NuGet.CommandLine if different:

    .packagesNuGet.CommandLine.2.8.6	oolsNuGet.exe Install -NonInteractive -OutputDirectory packages .nugetpackages.config
    

    For me, the output was:

    Installing 'OptimizedPriorityQueue 1.0.0.0'.
    Successfully installed 'OptimizedPriorityQueue 1.0.0.0'.
    All packages listed in packages.config are already installed.
    

  8. Right click on the project in Visual Studio and select Properties. Under Common Properties > References, click the Add New Reference… button.
  9. Select Browse on the left hand side. Next to the Add Reference dialog's OK and Cancel buttons, there is a Browse… button. Click that to open a file selection dialog.
  10. Navigate to the DLLs that NuGet unpacked to the packages subdirectory of your project folder and click the Add button. Click OK to close the Add Reference dialog.
  11. You should now be able to use the assembly in your C++/CLI project:

    using namespace Priority_Queue;
    
    //...
    

这篇关于如何使我的托管 NuGet 包支持 C++/CLI 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)