简体   繁体   English

如何使用 MSBuild NuGet 将 .csproj 文件加载到 .NET Core 3.0

[英]How to Load .csproj file into .NET Core 3.0 using MSBuild NuGet

I have a very simple console project:我有一个非常简单的控制台项目:

class Program
{
    static void Main(string[] args)
    {
        var project = new Project(
            "FishStory.csproj", 
            null, 
            null, 
            new ProjectCollection());

        Console.ReadLine();
    }
}

This is a .NET Core 3.0 console application, and it has Microsoft.Build (16.4.0) NuGet package referenced.这是一个 .NET Core 3.0 控制台应用程序,它引用了Microsoft.Build (16.4.0) NuGet 包。

The .csproj file FishStory.csproj is found (I don't get an exception that the .csproj is missing), but I do get the following error.找到了 .csproj 文件FishStory.csproj (我没有收到 .csproj 丢失的异常),但我确实收到以下错误。

Microsoft.Build.Exceptions.InvalidProjectFileException: 'The imported project "C:\\Users\\vchel\\source\\repos\\ForDave\\ForDave\\bin\\Debug\\netcoreapp3.0\\Microsoft.CSharp.targets" was not found. Microsoft.Build.Exceptions.InvalidProjectFileException: '未找到导入的项目“C:\\Users\\vchel\\source\\repos\\ForDave\\ForDave\\bin\\Debug\\netcoreapp3.0\\Microsoft.CSharp.targets”。 Confirm that the expression in the Import declaration "C:\\Users\\vchel\\source\\repos\\ForDave\\ForDave\\bin\\Debug\\netcoreapp3.0\\Microsoft.CSharp.targets" is correct, and that the file exists on disk.确认导入声明“C:\\Users\\vchel\\source\\repos\\ForDave\\ForDave\\bin\\Debug\\netcoreapp3.0\\Microsoft.CSharp.targets”中的表达式正确,并且该文件存在于磁盘上。 C:\\Users\\vchel\\source\\repos\\ForDave\\ForDave\\bin\\Debug\\netcoreapp3.0\\FishStory.csproj' C:\\Users\\vchel\\source\\repos\\ForDave\\ForDave\\bin\\Debug\\netcoreapp3.0\\FishStory.csproj'

I'd expect that such a simple MSBuild test would "just work", but it seems like I'm missing something.我希望这样一个简单的 MSBuild 测试会“正常工作”,但似乎我错过了一些东西。 What can I do to load this .csproj file?我该怎么做才能加载这个 .csproj 文件?

It seems the NuGet package adds the necessary .dlls to use the Project object, but the various project types must have .target and .props files which are used when the Project .csproj is loaded.似乎 NuGet 包添加了必要的 .dll 以使用 Project 对象,但各种项目类型必须具有加载 Project .csproj 时使用的 .target 和 .props 文件。

To load my specific project, I had to add the following files to the output directory.要加载我的特定项目,我必须将以下文件添加到输出目录中。 I accomplished this by placing the files in my project and marking them as Copy if Newer .我通过将文件放在我的项目中并将它们标记为Copy if Newer来完成此操作。

I added the following files:我添加了以下文件:

  • Microsoft.Common.targets Microsoft.Common.targets
  • Microsoft.CSharp.targets Microsoft.CSharp.targets
  • Microsoft.NETFramework.props Microsoft.NETFramework.props
  • Microsoft.NETFramework.targets Microsoft.NETFramework.targets

In my case the .csproj is a MonoGame project, so I also had to add:就我而言,.csproj 是一个 MonoGame 项目,所以我还必须添加:

  • MonoGame.Build.Tasks.dll (not sure if I needed this or not) MonoGame.Build.Tasks.dll(不确定我是否需要这个)
  • MonoGame.common.props MonoGame.common.props
  • MonoGame/v3.0/MonoGame.Content.Builder.targets MonoGame/v3.0/MonoGame.Content.Builder.targets

To add these files so they are copied to the output folder:添加这些文件以便将它们复制到输出文件夹:

  1. Add the file to your project (for .NET Core you just have to add the file to the directory)将文件添加到您的项目中(对于 .NET Core,您只需将文件添加到目录中)
  2. Right-click on the file in the Solution Explorer and select Properties右键单击解决方案资源管理器中的文件并选择属性
  3. Set the Copy to Output Directory to Copy if newer如果较新复制到输出目录设置为复制

I pulled the targets/props/.dll files from:我从以下位置提取了目标/道具/.dll 文件:

  • C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319 C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319
  • C:\\Program Files (x86)\\MSBuild\\MonoGame\\v3.0 C:\\Program Files (x86)\\MSBuild\\MonoGame\\v3.0

I am guessing that other project types (such as an Android Xamarin project) may require different .targets files.我猜测其他项目类型(例如 Android Xamarin 项目)可能需要不同的 .targets 文件。

Finally, I also had to manually add the NuGet Package Microsoft.Build.Utilities.Core .最后,我还必须手动添加 NuGet 包Microsoft.Build.Utilities.Core Not sure why that wasn't automatically added when adding Microsoft.Build不知道为什么在添加Microsoft.Build时没有自动添加

解决方案资源管理器截图

I have catched the same exception while I have tried to load a project targeted to netcore31 platform.我在尝试加载针对 netcore31 平台的项目时遇到了同样的异常。
But after that I have installed但在那之后我已经安装了

  • Microsoft.Build.Utilities.Core(16.5.0) Microsoft.Build.Utilities.Core(16.5.0)
  • Microsoft.Build (16.4.0) Microsoft.Build (16.4.0)

nuget packages, there is no more the exception. nuget 包,没有更多的例外。 The project is loaded successfully.项目加载成功。

Maybe, it will help someone more.也许,它会帮助更多人。

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

相关问题 如何以编程方式使用 .net 2.0 MSBuild 构建 csproj 文件? - How to build a csproj file using .net 2.0 MSBuild programmatically? MSBuild nuget 包:要添加到 .csproj 文件中的内容? - MSBuild nuget package: what to add to .csproj file? 在 nuget 包中包含引用的项目 DLL [.Net Core RC3 *.csproj 文件] - Including referenced project DLLs in nuget package [.Net Core RC3 *.csproj file] .NET Core + MSBuild:如何将csproj与多个project.json文件关联起来? - .NET Core + MSBuild: How to associate a csproj with multiple project.json files? 如何生成.net内核或.net标准csproj文件? - How to generate .net core or .net standard csproj file? 如何在.NET Core 2 classlib项目中使用C#从.csproj文件读取/获取PropertyGroup值? - How to read/get a PropertyGroup value from a .csproj file using C# in a .NET Core 2 classlib project? 获取包含在 csproj in.Net Core 3.1 中的 nuget package 版本 - Get nuget package version included in csproj in .Net Core 3.1 使用 csProj 发布到 NuGet:如何添加元数据? - Publishing to NuGet using csProj: How to add Metadata? 使用.NET核心库csproj中的Selenium WebDriver - Using Selenium WebDriver in library csproj with .NET Core 如何使用MSBuild将链接文件添加到csproj文件。 (3.5框架) - How to add a linked file to a csproj file with MSBuild. (3.5 Framework)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM