如何隐藏依赖的nuget软件包?

发布于 2025-02-09 13:30:13 字数 935 浏览 2 评论 0 原文

我有一个类似的类库项目文件:

<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>

我的类库有各种各样的公共类型,例如 publyClass1 。 nuget软件包 inoverylog 具有名为日志的公共类型。我的班级库需要 log 在内部工作,但我不想让我的类库用户可见这种类型。我希望人们可以访问 uplyClass1 ,但不能 log InformationLog 中的其他任何类型或名称空间。

我可以将 inoverylog 作为依赖项,强迫我的类库的项目也使用 inoverylog nuget软件包,我只是不希望其中的类型Nuget软件包是可见的。

理想情况下,我希望项目文件中的一些XML解决此问题。这样的事情:

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

有没有办法做这样的事情?

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. The Nuget package InformationLog has a public type named 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. I want people to have access to PublicClass1 but not to Log or any of the other types or namespaces in InformationLog.

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.

Ideally I would like some XML in my project file to fix this. Something like this:

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

Is there a way to do something like that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

維他命╮ 2025-02-16 13:30:13

您可以使用 privateasets =“ compile”

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

还有更多细节。

You can see that Roslyn, and others, also take advantage of this:

通过不制作 Runtime Asset Private,它意味着运行时资产会流过运输,并且会流向运输,并且依赖项将被复制到apps bin/文件夹,并避免使用依赖项上的汇编调用API时避免使用fileNotfound异常。但是,由于编译资产是您项目的私有的,因此它们不会传递流动,因此,包装消费者将无法直接在包裹的依赖项上调用API,除非他们自己添加了Packagagerference相同的软件包,使其成为直接参考,而不是传递包。

You can use PrivateAssets="compile".

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

NuGet's docs on asset selection has some more details.

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

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文