如何在.csproj文件中引用NuGet包

3
我需要在我的.NET Framework控制台应用程序的.csproj文件中使用<attrib> [文档]元素。
它嵌套在<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>

当代码写成这样时,编辑器会给我这个错误:任务'Attrib'未定义我尝试了什么? 我会使用<UsingTask>元素,其中参数是NAnt.Core NuGet包的路径。整个代码如下: <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> 但错误并没有消失。当我尝试编译应用程序时,我得到以下错误: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.
1个回答

2
我认为您混淆了NAnt和MSBuild任务。
NAnt任务是在.build文件中编写的,并通过将此文件传递给NAnt可执行文件来调用,就像这里所解释的那样。您可以使用loadtasks来加载它们。
然而,MSBuild任务可以像您想要的那样在.csproj文件中使用。 您可以使用usingtask来处理它们。
所以在您的情况下,您可以使用msbuildtasks包,它还具有一个attrib任务。
安装该包:
引用块:

最新版本可从发布版中下载。 https://github.com/loresoft/msbuildtasks/releases

MSBuild Community Tasks库也可通过nuget.org包名> MSBuildTasks获得。

要安装MSBuildTasks,请在程序包管理器控制台中运行以下命令

PM> Install-Package MSBuildTasks

安装还确保您可以在csproj中使用任务而无需使用usingtask,因此:
<Target Name="BeforeBuild">
    <Attrib Files="App.config" ReadOnly="false" />
    <Attrib Files="Ocelot.json" ReadOnly="false" />
    <Attrib Files="OcelotLogging.json" ReadOnly="false" />
</Target>

请注意,使用MSBuild还有其他方法来实现此操作,但这种方法与您编写的最接近。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接