microsoft / MSBuildLocator

An API to locate MSBuild assemblies from an installed Visual Studio location. Use this to ensure that calling the MSBuild API will use the same toolset that a build from Visual Studio or msbuild.exe would.
Other
212 stars 83 forks source link

Any Powershell example? #205

Closed greenkalx closed 11 months ago

greenkalx commented 1 year ago

I'm trying to use MSBuildLocator in Powershell with the end goal of parsing C# and C++ projects with ProjectLoader in the dotnet samples: https://github.com/dotnet/samples/blob/main/github-actions/DotNet.GitHubAction/DotNet.CodeAnalysis/ProjectLoader.cs

The Powershell script below fails to load the locator assembly, with error: Add-Type : Could not load file or assembly '.\\Microsoft.Build.Locator' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

Are there any examples of this?

$assemblies = (".\Microsoft.Build.Locator")

$id = get-random
$code = @"
using System;
using Microsoft.Build;
using Microsoft.Build.Locator;

namespace HelloWorld
{
    public class Program$id
    {
        public static void Main()
        {
            Console.WriteLine("Hello world!");

            MSBuildLocator.RegisterDefaults();
        }
    }
}
"@

Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharp   
iex "[HelloWorld.Program$id]::Main()"
Forgind commented 1 year ago

That seems to be an error with finding MSBuildLocator, not an error with using MSBuildLocator. (Your code, though it doesn't do much, looks correct.) See https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/add-type?view=powershell-7.3#-referencedassemblies

YuliiaKovalova commented 11 months ago

Close since no customer reply.

Feel free to ping us in the ticket again if the issue still persists.