Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

c# - InvalidProjectFileException with using Microsoft.NET.Sdk in Microsoft.Build.Evaluation

I want to parse a C#-project to read out all project- and other references. For months, I used the Microsoft.Build.Evaluation namespace from the package Microsoft.Build.

var file = Directory.GetFiles(rootDirectory, "*.csproj").First();
var project = new Project(test);

For the old csproj-files it is working fine without any problem. The old files are starting with

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

Since migrating to the new SDK-Project style, I have some problems with reading the csproj files. They start with

<Project Sdk="Microsoft.NET.Sdk">

When loading the project I get the following exception: InvalidProjectFileException: 'The SDK 'Microsoft.NET.Sdk' specified could not be found. I figured out, if I change the Version of the package Microsoft.Build to the version of my Visual Studio/MSBuild (e.g. 16.8) it works fine. I have a lot of colleagues with a high variety of MSBuild Versions >15 and we cannot have all the same MSBuild version.

A possible solution which I have found is to add the Microsoft.Build.Runtime package but if I add it to my project I get a lot of compiler errors and no type is found which works before.

The project which should load the csproj files is a ToolsVersion="4.0"-project style with .NET Framework 4.7.2 (cannot change this to SDK project style because of massive T4 template usage).

question from:https://stackoverflow.com/questions/65845683/invalidprojectfileexception-with-using-microsoft-net-sdk-in-microsoft-build-eval

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...