简体   繁体   English

我如何创建一个(仅设计时)nuget程序包,该程序包在安装时不添加程序集引用?

[英]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.

相关问题 自定义nuget提要,VS未将软件包视为已安装 - Custom nuget feed, VS doesn't see package as installed NuGet Package 管理器控制台:“update-package -project ...”不会更新所有已安装的软件包 - NuGet Package Manager Console: “update-package -project …” doesn't update all installed packages 如何解决此Nuget软件包的安装问题? - How can I troubleshoot the installation of this Nuget package? NuGet项目&gt;添加库包参考未出现 - NuGet Project > Add Library Package Reference not appearing Nuget - 添加库 package 参考 - 在当前 package 源中找不到包 - Nuget - Add Library package reference - No packages found in the current package source 如何在安装后在Visual Studio 2010中创建安装包,它在启动时自动运行? - How do I create a installation package in visual studio 2010 after installed it is automatically run when boot? 如何在VS 2010中向控制台应用程序添加设计时服务引用? - How to add a design time service reference to a console application in VS 2010? Json.Net NuGet(NuPack)软件包参考无法找到与目标框架兼容的程序集 - Json.Net NuGet (NuPack) Package Reference unable to find assembly compatible with target framework 无法在T4模板中引用装配 - Can't reference an assembly in a T4 template 如何将嵌套的设计时属性添加到用户控件? - How can I add a nested design-time property to a user control?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM