[英]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
但不能访问Log
或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.我可以将
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.