复制C DLL以构建任何C#项目的输出,以扩展为参考

Copy C++ dlls to build output of any C# project that reference them by extension

本文关键字:输出 扩展 参考 DLL 构建 任何 复制 项目      更新时间:2023-10-16

我有一个C#库,该库引用了一些C++CLI项目,其中包含一些 C++库编译为dlls

因此,总的来说,在使用我的库的任何应用程序的运行时,我当然需要C#CLI dlls,但也需要所有C++,即使CLI Project引用了它们,默认情况下也不会复制它们。

使用预构建事件手动复制它们。

,但我不仅需要每个应用程序项目添加它们,而且我也想将我的库以NuGet发布,并且无法控制引用项目的属性。

有解决方法吗?特定于NuGet软件包的事件。

我喜欢Hans Passants评论。他的答案减少了复制数据的数量,并允许无缝使用AnyCPU。

  • 我的解决方案是WPF项目使用的东西。支持旧项目文件的项目类型,Nuget不太支持.NET CORE或.NET标准项目。但是.NET标准中没有C CLI。我们只有X64组件,我的消费者只有内部,因此不需要关心Win32。新的Nuget语法可以为这些DLL提供很大的控制,但实际上我对我的学习曲线和Nuget都没有完成。

所以这就是我对严格包装DLL的使用。人们可以调试我的DLL,我将调试DLL,PDB放入包装中,它们仅在我们公司内部消耗。如果用户将配置更改为调试,我确实编写了这些汇编的包装文件的目标文件。

本机Dll< - C CLI包装器< - C#便利载荷

'nuspec

<?xml version="1.0"?>
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <description></description>
    <id>yourId</id>
    <version>$version$</version>
    <authors></authors>    
    <!-- Only done because at the beginning I copied the native dll into the lib/net461 folder which gets automatically referenced -->
    <references>
      <reference file="yourCSharpProject.dll" />
      <reference file="yourInteropProject.dll" /> <!-- if needed -->
    </references>
  </metadata>
  <files>
    <!-- the target file will copy my native dlls into the target file  -->
    <file src="target**" target="build"/>
    <!-- copy all debug and release of ... Native.dlls, pdbs, xml -->
    <!-- usage hint ** preserves the folder structure, debug and release  -->
    <file src="binx64**YourNative.*" target="libnative" />  

    <!-- copy all debug and release of ... Interop.lls, pdbs, xml -->
    <file src="binx64Release.*" target="libnet461" />  
  </files>
</package>

'target yourpackagename.target

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="CopyBinaries" BeforeTargets="BeforeBuild">
    <CreateItem Include="$(MSBuildThisFileDirectory)../lib/native/$(Configuration)/*" >
      <Output TaskParameter="Include" ItemName="SomeImportantName" />
    </CreateItem>
    <Copy SourceFiles="@(SomeImportantName)" 
          DestinationFolder="$(Outputpath)" 
          SkipUnchangedFiles="true" />
  </Target>
</Project>