JHBonarius
JHBonarius

Reputation: 11271

Unable to find type [Microsoft.Build.Construction.SolutionFile]

I've suddenly got a problem with a script that has been running without problems previously

$sln = 'D:\[...]\[...].sln'
Add-Type -Path (${env:ProgramFiles(x86)} + '\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\Microsoft.Build.dll')
$slnFile = [Microsoft.Build.Construction.SolutionFile]::Parse($sln)

The Add-Type seems to work without an error, but the next line keeps throwing an error

Unable to find type [Microsoft.Build.Construction.SolutionFile].
At D:\[...]\[scriptname].ps1:7 char:13
+     $slnFile = [Microsoft.Build.Construction.SolutionFile]::Parse($sl ...
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Build...on.SolutionFile:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

The script was based on several examples

And it worked in the past. I'm still pointing to the same old Microsoft.Build.dll assembly. Can anybody explain to me what's changed?

Upvotes: 1

Views: 502

Answers (1)

Rick
Rick

Reputation: 1

the question is over 3 years old, but I think it's still relevant now and again. Firstly, MSBuild is very version-dependent. So you always have to pay attention to which MSBuild version you are using. I noticed that although Microsoft still lists and describes the Microsoft.Build.Construction namespace in the description, the SolutionFile class no longer exists. I referenced MSBuild V4.0.0.0 in Visual Studio 2017. That's probably the problem here too.

Update: You can download the NuGet-Package Microsoft.Build for the current opened project in the according version via "Tools"=>"NuGet Package Manager"=>"Manage NuGet Packages for Solution ..." over Visual Studio menu. The setup replaces the Pre-Installed MSBuild references. OR you should add Microsoft.Build references from the section "Extensions" which is the current version for the Visual Studio version.

Upvotes: 0

Related Questions