在NuGet包中将嵌入互操作类型设置为false

3

我需要在我的项目中引用一个程序集,但是为了成功编译,需要将“嵌入互操作类型”设置为false。如果我不选择这个选项,就会出现编译时错误:

A reference was created to embedded interop assembly 'Interop.MSTSCLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because of an indirect reference to that assembly created by assembly 'AxInterop.MSTSCLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Consider changing the 'Embed Interop Types' property on either assembly.

如果只是一个项目,我会将选项设置为false而不必担心,但现在我正在制作一个nuget包,我正在尝试找到一种方法在“添加包”时设置该选项。我的nuspec文件目前如下(省略元数据):
<files>
  <file src="Interop.MSTSCLib.dll" target="lib\net40" />
  <file src="AxInterop.MSTSCLib.dll" target="lib\net40" />
</files>

我查看了NuGet的文档,但没有找到如何做到这一点。这是否可能?
1个回答

6

您需要在tools目录下放置一个install.ps1文件,类似于:

param($installPath, $toolsPath, $package, $project)

$project.Object.References | Where-Object { $_.Name -eq "Interop.MSTSCLib" } |  ForEach-Object { $_.EmbedInteropTypes = $false }
$project.Object.References | Where-Object { $_.Name -eq "AxInterop.MSTSCLib" } |  ForEach-Object { $_.EmbedInteropTypes = $false }

基于这个问题Can NuGet distribute a COM dll?以及它的答案,区别在于你不需要调用regsvr32,因为你不需要分发和注册com dll。


1
太棒了,尽管install.ps1似乎不再受支持。我改用了init.ps1,并将其放在tools中,它完美地工作了! - Philippe Paré
根据这个问题,install.ps1的支持未来似乎不确定。如果您的项目使用PackageReference节点,只有init.ps1将得到支持。目前,对PackageReference节点的支持受限于少数几种项目类型(.Net Core、.Net Standard和Visual Studio 2017中的UWP)。 - mcdon
2
现在推荐的方法是使用Nuget对MSBuild目标文件的支持。请参见: Nuget.Samples.Interop - CyberMonk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接