简体   繁体   English

如何在 .csproj 中引用 NuGet 包

[英]How to reference NuGet package in .csproj

I need to use <attrib>[Docs] element in the .csproj file of my .NET Framework console application.我需要在我的 .NET Framework 控制台应用程序的 .csproj 文件中使用<attrib>[Docs]元素。

It's nested in the <Target Name="BeforeBuild"> element because I need to edit attributes of a few files before building, here is the full code from the .csproj:它嵌套在<Target Name="BeforeBuild">元素中,因为我需要在构建之前编辑几个文件的属性,这里是 .csproj 中的完整代码:

<Target Name="BeforeBuild">
    <Attrib Files="App.config" ReadOnly="false" />
    <Attrib Files="Ocelot.json" ReadOnly="false" />
    <Attrib Files="OcelotLogging.json" ReadOnly="false" />
</Target>

When the code is written as this, editor gives me this error: Task 'Attrib' is not defined .当代码写成这样时,编辑器给了我这个错误: Task 'Attrib' is not defined

What have I tried?我尝试了什么?

  • I would use the <UsingTask> element, where the parameter is the path to the NAnt.Core NuGet package.我将使用<UsingTask>元素,其中参数是 NAnt.Core NuGet 包的路径。 Whole code looks like this:整个代码如下所示:

    <UsingTask TaskName="Attrib" AssemblyFile="C:\\Users\\UserName\\.nuget\\packages\\nant.core\\0.92.0\\lib\\net40\\NAnt.Core.dll" /> <Target Name="BeforeBuild"> <Attrib Files="App.config" ReadOnly="false" /> <Attrib Files="Ocelot.json" ReadOnly="false" /> <Attrib Files="OcelotLogging.json" ReadOnly="false" /> </Target> But the error doesn't dissapear. <UsingTask TaskName="Attrib" AssemblyFile="C:\\Users\\UserName\\.nuget\\packages\\nant.core\\0.92.0\\lib\\net40\\NAnt.Core.dll" /> <Target Name="BeforeBuild"> <Attrib Files="App.config" ReadOnly="false" /> <Attrib Files="Ocelot.json" ReadOnly="false" /> <Attrib Files="OcelotLogging.json" ReadOnly="false" /> </Target>但错误并没有消失。 When I try to compile the application anyway, I got following error: The "Attrib" task could not be loaded from the assembly C:\\Users\\UserName\\.nuget\\packages\\nant.core\\0.92.0\\lib\\net40\\NAnt.Core.dll. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.无论如何,当我尝试编译应用程序时,出现以下错误: The "Attrib" task could not be loaded from the assembly C:\\Users\\UserName\\.nuget\\packages\\nant.core\\0.92.0\\lib\\net40\\NAnt.Core.dll. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. The "Attrib" task could not be loaded from the assembly C:\\Users\\UserName\\.nuget\\packages\\nant.core\\0.92.0\\lib\\net40\\NAnt.Core.dll. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

I think you are mixing NAnt and MSBuild tasks.我认为您正在混合 NAnt 和 MSBuild 任务。

NAnt tasks are written on a .build file and called by passing this file to the NAnt executable like explained here . NAnt 任务写在一个 .build 文件中,并通过将这个文件传递给 NAnt 可执行文件来调用,就像这里解释的那样。 You load them with loadtasks .你用loadtasks加载它们。

MSBuild tasks however can be used like you want, in the .csproj file.但是,可以在 .csproj 文件中随意使用MSBuild 任务 You use usingtask to work with them.您可以使用usingtask来处理它们。

So in your case, what you can do is use the msbuildtasks package which also have an attrib task.所以在你的情况下,你可以做的是使用msbuildtasks 包,它也有一个attrib任务。

Install the package:安装软件包:

The latest build can be downloaded from the releases section.最新版本可以从发布部分下载。 https://github.com/loresoft/msbuildtasks/releases https://github.com/loresoft/msbuildtasks/releases

The MSBuild Community Tasks library is also available on nuget.org via package name > MSBuildTasks. MSBuild 社区任务库也可在 nuget.org 上通过包名称 > MSBuildTasks 获得。

To install MSBuildTasks, run the following command in the Package Manager Console要安装 MSBuildTasks,请在包管理器控制台中运行以下命令

PM> Install-Package MSBuildTasks

The installation also make sure you can then use the tasks in your csproj without needing to use usingtask , so:安装还确保您可以使用 csproj 中的任务而无需使用usingtask ,因此:

<Target Name="BeforeBuild">
    <Attrib Files="App.config" ReadOnly="false" />
    <Attrib Files="Ocelot.json" ReadOnly="false" />
    <Attrib Files="OcelotLogging.json" ReadOnly="false" />
</Target>

Note they are other way to do this with MSBuild, this one is only the closest to what you wrote.请注意,它们是使用 MSBuild 执行此操作的其他方法,这只是与您编写的内容最接近的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM