References in SDK-based projects are fully transitive so - similar to many other package managers like npm or maven - you all the transitive references are available in the project to make sure the app compiles and runs cleanly, e.g. there are no unresolved references when the dependency is referenced and all assemblies are part of the build output and ready to run. (there may even be conflict resolution applied to conflicting version of assemblies resulting in the generation of binding redirects.)
In previous versions, you would need to install NuGet packages or add additional project references to other projects as well to not get build errors or type load exceptions.
如果您希望您的项目能够正确运行和解决冲突但不将传递引用传递给编译器,那么目前没有完美的解决方法。
如果您只需要依赖项来构建项目,而不需要运行它,您可以将包或项目引用标记为PrivateAssets="All"
(将作为属性添加到 .csproj 文件中的引用)。
如果您想强制使用 API - 例如对于分层 API,请考虑编写一个 roslyn 分析器,如果您从不想要的地方引用 API,它将发出警告。这可能适用于需要工具来维护所需架构的大型项目。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句