[英]How can i create a (design time only) nuget package that doesn't add an assembly reference when installed?
I want to build a nuget
package for a design time only package. 我想为仅设计时间的程序包构建一个nuget
程序包。 This package contains resources (a ruleset file, a build props file and an assembly) for using a custom code analysis rule set. 该软件包包含用于使用自定义代码分析规则集的资源(规则集文件,构建道具文件和程序集)。
My nuspec file (inside my CodeAnalysis
assembly project) looks like this: 我的nuspec文件(在我的CodeAnalysis
程序集项目中)如下所示:
<package>
<metadata>
<id>$id$</id>
<version>$version$</version>
<authors>$author$</authors>
<description>$description$</description>
<references>
</references>
<developmentDependency>true</developmentDependency>
</metadata>
<files>
<!-- copy rules assembly to tools folder -->
<file src="bin\Release\CodeAnalysis.dll" target="tools\CodeAnalysis.dll" />
<file src="tools\CodeAnalysis.ruleset" target="tools\CodeAnalysis.ruleset" />
<file src="build\CodeAnalysis.props" target="build\CodeAnalysis.props" />
</files>
</package>
When in build this package, the CodeAnalysis.dll
assembly is present in \\lib\\net40\\CodeAnalysis.dll
, and a copy is added in \\tools\\CodeAnalysis.dll
. 在构建此程序包时, \\lib\\net40\\CodeAnalysis.dll
存在CodeAnalysis.dll
程序集,并且在\\tools\\CodeAnalysis.dll
添加了一个副本。
When i use this package in another project, the default behavior is that a reference to the assembly is added to the project. 当我在另一个项目中使用此程序包时,默认行为是对该程序集的引用被添加到该项目中。 Since this aa design / build time only assembly (only used when executing code analysis on the project), i don't want nuget
to add this reference. 由于这是仅设计/构建时的程序集(仅在对项目执行代码分析时使用),因此我不想nuget
添加此引用。
I've looked into the <references>
section of the nuspec
file, but that doesn't give me a clue how to prevent a reference from being made. 我已经查看了nuspec
文件的<references>
部分,但这并没有为我提供如何防止创建引用的线索。
How do i build a nuspec file so that nuget does not add a project reference when installing the nuget package? 我如何构建nuspec文件,以便在安装nuget软件包时nuget不会添加项目引用?
Take a look at the ConditionalAttribute class in the System.Diagnostics namespace. 看一下System.Diagnostics命名空间中的ConditionalAttribute类。 With this you can specify a conditional compilation symbol that if isn't defined the target of the [Conditional]
attribute isn't compiled into the resulting assembly. 这样,您可以指定一个条件编译符号,如果未定义,则不会将[Conditional]
属性的目标编译到结果程序集中。
This way you could define a CODE_ANALYSIS
symbol (or whatever name you choose) and decorate all the types, methods, properties, etc. in your project with the [Conditional("CODE_ANALYSIS")]
attribute. 这样,您可以定义CODE_ANALYSIS
符号(或您选择的任何名称),并使用[Conditional("CODE_ANALYSIS")]
属性装饰项目中的所有类型,方法,属性等。 If people using your package haven't also defined this symbol then their resulting compiled assembly will not include a reference to your project, but they will be able to benefit from it at design time. 如果使用您的软件包的人还没有定义此符号,那么他们生成的编译后的程序集将不包含对您的项目的引用,但是他们将能够在设计时从中受益。
This is what JetBrains use for their Annotations NuGet package , there's a blog post about it here: 这就是JetBrains用于其Annotations NuGet包的方式 ,这里有一篇博客文章 :
http://blog.jetbrains.com/dotnet/2015/08/12/how-to-use-jetbrains-annotations-to-improve-resharper-inspections/ http://blog.jetbrains.com/dotnet/2015/08/12/how-to-use-jetbrains-annotations-to-improve-resharper-inspections/
Pass the -tool
command line switch to nuget. 将-tool
命令行开关传递给nuget。 This will not include the DLL as a referenced assembly. 这将不包括DLL作为参考程序集。
nuget pack MyProject.csproj -tool
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.