[英]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:我添加了以下文件:
In my case the .csproj is a MonoGame project, so I also had to add:就我而言,.csproj 是一个 MonoGame 项目,所以我还必须添加:
To add these files so they are copied to the output folder:添加这些文件以便将它们复制到输出文件夹:
I pulled the targets/props/.dll files from:我从以下位置提取了目标/道具/.dll 文件:
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但在那之后我已经安装了
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.