Skip to content

Instantly share code, notes, and snippets.

@HellBrick
Created November 3, 2015 21:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HellBrick/8d3a6825de12035f3733 to your computer and use it in GitHub Desktop.
Save HellBrick/8d3a6825de12035f3733 to your computer and use it in GitHub Desktop.
Roslyn analyzer test console
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HellBrick.Diagnostics.UnusedReferences;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.MSBuild;
namespace TestConsole
{
internal class Program
{
public static void Main( string[] args )
{
MainAsync( args ).GetAwaiter().GetResult();
}
public static async Task MainAsync( string[] args )
{
const string solutionPath = @"F:\Software\AsyncCollections\AsyncCollections.sln";
Solution solution = await MSBuildWorkspace.Create().OpenSolutionAsync( solutionPath ).ConfigureAwait( false );
UnusedReferencesAnalyzer analyzer = new UnusedReferencesAnalyzer();
ImmutableArray<DiagnosticAnalyzer> analyzers = ImmutableArray.Create<DiagnosticAnalyzer>( analyzer );
foreach ( Project project in solution.Projects )
{
Compilation compilation = await project.GetCompilationAsync().ConfigureAwait( false );
CompilationWithAnalyzers compilationWithAnalyzers = compilation.WithAnalyzers( analyzers );
ImmutableArray<Diagnostic> diagnostics = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().ConfigureAwait( false );
Console.WriteLine( $"{project.Name}: {diagnostics.Length} diagnostics" );
foreach ( Diagnostic diagnostic in diagnostics )
Console.WriteLine( diagnostic );
Console.WriteLine( "----------------------------------------" );
}
}
}
}
@mungojam
Copy link

mungojam commented Jan 28, 2018

Thanks for putting this together, which nuget package will add Microsoft.CodeAnalysis.MSBuild? I've tried about 10 different rolsyn related ones

Update: not to worry, I was running in .net core which it seems isn't supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment