| title | 使用 COM 互操作程序集创建包 |
|---|---|
| description | 介绍如何创建包含 COM 互操作程序集的包 |
| author | karann-msft |
| ms.author | karann |
| ms.date | 07/09/2019 |
| ms.topic | conceptual |
| ms.openlocfilehash | de164b136a1636b89f674b8626613094fc53e04c |
| ms.sourcegitcommit | 2b50c450cca521681a384aa466ab666679a40213 |
| ms.translationtype | HT |
| ms.contentlocale | zh-CN |
| ms.lasthandoff | 04/07/2020 |
| ms.locfileid | 75385567 |
包含 COM 互操作程序集的包必须包含合适的目标文件,使正确的 EmbedInteropTypes 元数据使用 PackageReference 格式添加到项目。 默认情况下,使用 PackageReference 时,EmbedInteropTypes 元数据对所有程序集一直为 false,所以目标文件显式添加此元数据。 为了避免冲突,目标名称必须是唯一的;理想情况下,使用包名称和嵌入程序集的组合,使用此值替换下例中的 {InteropAssemblyName}。 (有关示例,另请参阅 NuGet.Samples.Interop。)
<Target Name="Embedding**AssemblyName**From**PackageId**" AfterTargets="ResolveReferences" BeforeTargets="FindReferenceAssembliesForReferences">
<ItemGroup>
<ReferencePath Condition=" '%(FileName)' == '{InteropAssemblyName}' AND '%(ReferencePath.NuGetPackageId)' == '$(MSBuildThisFileName)' ">
<EmbedInteropTypes>true</EmbedInteropTypes>
</ReferencePath>
</ItemGroup>
</Target>请注意,在使用 packages.config 管理格式时,将引用从包添加到程序集会导致 NuGet 和 Visual Studio 检查 COM 互操作程序集并在项目文件中将 EmbedInteropTypes 设为 true。 在这种情况下,目标被替代。
此外,默认情况下,生成资产不以可传递的方式流动。 当此处所述的创作的包作为可传递的依赖项从项目拉取到项目引用时,它们会以不同的方式工作。 包使用者通过将 PrivateAssets 默认值修改为不包含生成使其流动。