我有一个类似的类库项目文件:
<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?
发布评论
评论(1)
您可以使用
privateasets =“ compile”
。还有更多细节。
You can see that Roslyn, and others, also take advantage of this:
通过不制作
Runtime
Asset Private,它意味着运行时资产会流过运输,并且会流向运输,并且依赖项将被复制到appsbin/
文件夹,并避免使用依赖项上的汇编调用API时避免使用fileNotfound异常。但是,由于编译
资产是您项目的私有的,因此它们不会传递流动,因此,包装消费者将无法直接在包裹的依赖项上调用API,除非他们自己添加了Packagagerference相同的软件包,使其成为直接参考,而不是传递包。You can use
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 appsbin/
folders and avoid FileNotFound exceptions when your assembly calls APIs on the dependency. But since thecompile
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.