简体   繁体   English

如何隐藏依赖的 Nuget 包?

[英]How can I hide dependent Nuget packages?

I have a class library project file like this:我有一个这样的类库项目文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="InformationLog" Version="2.1.1" />
  </ItemGroup>

</Project>

My class library has various public types, such as PublicClass1 .我的类库有各种公共类型,例如PublicClass1 The Nuget package InformationLog has a public type named Log . Nuget 包InformationLog有一个名为Log的公共类型。 My class library needs Log internally to work, but I don't want to make this type visible to users of my class library.我的类库需要在内部使用Log才能工作,但我不想让我的类库的用户看到这种类型。 I want people to have access to PublicClass1 but not to Log or any of the other types or namespaces in InformationLog .我希望人们可以访问PublicClass1但不能访问LogInformationLog中的任何其他类型或命名空间。

I'm OK with keeping InformationLog as a dependency, forcing projects that reference my class library to also use the InformationLog nuget package, I just don't want the types in that Nuget package to be visible.我可以将InformationLog保留为依赖项,强制引用我的类库的项目也使用InformationLog nuget 包,我只是不希望该 Nuget 包中的类型可见。

Ideally I would like some XML in my project file to fix this.理想情况下,我希望我的项目文件中有一些 XML 来解决这个问题。 Something like this:像这样的东西:

  <PackageReference Include="InformationLog" Version="1.9.0" Access="Private"/>

Is there a way to do something like that?有没有办法做这样的事情?

You can use PrivateAssets="compile" .您可以使用PrivateAssets="compile"

<PackageReference Include="InformationLog" Version="1.9.0" PrivateAssets="compile"/>

NuGet's docs on asset selection has some more details. NuGet 关于资产选择的文档有更多详细信息。

You can see that Roslyn, and others, also take advantage of this: https://github.com/dotnet/roslyn-sdk/blob/1df5d394195ffcf6aab5b824ae420197ed1f0acc/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/Microsoft.CodeAnalysis.Analyzer.Testing.csproj#L58-L62您可以看到 Roslyn 和其他人也利用了这一点: https ://github.com/dotnet/roslyn-sdk/blob/1df5d394195ffcf6aab5b824ae420197ed1f0acc/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/Microsoft .CodeAnalysis.Analyzer.Testing.csproj#L58-L62

By not making the runtime asset private, it means that runtime assets will flow transitively, and the dependency will be copied to apps bin/ folders and avoid FileNotFound exceptions when your assembly calls APIs on the dependency.通过不将runtime资产设为私有,这意味着运行时资产将传递流动,并且依赖项将被复制到应用程序的bin/文件夹中,并避免在程序集调用依赖项上的 API 时出现 FileNotFound 异常。 But since the compile assets are private to your project, they will not flow transitively, and therefore the package consumer will not be able to call APIs on your package's dependencies directly, unless they themselves add a PackageReference to the same package, making it a direct reference, not a transitive package.但是由于compile资源对您的项目是私有的,因此它们不会传递,因此包使用者将无法直接调用包依赖项的 API,除非他们自己将 PackageReference 添加到同一个包,使其成为直接参考,而不是传递包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决 NuGet 包的这个问题? - How can I solve this problem of NuGet packages? 如何在我的 iOS 和 Android 项目中用 .NET MAUI NuGet 包替换我的 Xamarin NuGet 包? - How can I replace my Xamarin NuGet packages with .NET MAUI NuGet packages in my iOS and Android projects? 删除 NuGet 包的依赖 NuGet 包 - Removing a NuGet package's dependent NuGet packages 如何在 VSCode 中为 nuget 包配置本地提要? - How can I configure a local feed for nuget packages in VSCode? 如何抑制nuget包中“垃圾”的产生? - How can I suppress the generation of 'trash' from nuget packages? 如何在 Azure Functions 中使用 NuGet 包? - How can I use NuGet packages in my Azure Functions? 如何隐藏或限制用户仅从nuget下载相关软件包? - How to hide or restrict user to download only dependent package from nuget? 我应该如何构建NuGet软件包? - How am I supposed to be building NuGet packages? 如何根据给定的NuGet包列出当前解决方案中的所有项目? - How can I list all projects in the current solution dependent on a given NuGet package? 如何调试并逐步进入已创建并在本地托管的NuGet软件包中的代码? - How can I debug and step into code that's in NuGet packages that I've created and are hosted locally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM