Я пытаюсь использовать Roslyn для анализа очень простого решения на C #, консольного приложения с простой скелетной программой:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CA5
{
class Program
{
static void Main(string[] args)
{
}
}
}
Приведенный выше код создается без ошибок в VS 2019 Preview. Для такого простого решения / программы я ожидал, что Roslyn CA будет работать без сбоев, однако компиляция Roslyn возвращает ошибки, большинство из которых связано с нераспознанным типом.
Мой код CA такой:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
namespace CA5X
{
class Program
{
static void Main(string[] args)
{
const string path = @"C:\Utils\CA5\CA5.sln";
var props = new Dictionary<string, string>();
props["CheckForSystemRuntimeDependency"] = "true";
MSBuildWorkspace buildWorkspace = MSBuildWorkspace.Create(props);
Solution solutionToAnalyze = buildWorkspace.OpenSolutionAsync(path).Result;
foreach (Microsoft.CodeAnalysis.Project sProject in solutionToAnalyze.Projects)
{
Compilation sCompilation = sProject.GetCompilationAsync().Result;
var errors = sCompilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error);
Console.WriteLine("COUNT: " + errors.Count().ToString());
foreach (Microsoft.CodeAnalysis.Diagnostic diagnostic in errors)
{
Console.WriteLine(diagnostic.GetMessage());
}
}
}
}
}
Диагностика, полученная после компиляции, такова:
The type or namespace name 'AssemblyTitleAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyTitle' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyDescriptionAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyDescription' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyConfigurationAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyConfiguration' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCompanyAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCompany' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyProductAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyProduct' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCopyrightAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCopyright' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyTrademarkAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyTrademark' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCultureAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyCulture' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'ComVisibleAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'ComVisible' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'GuidAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'Guid' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyVersionAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyVersion' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyFileVersionAttribute' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'AssemblyFileVersion' could not be found (are you missing a using directive or an assembly reference?)
Predefined type 'System.String' is not defined or imported
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
Predefined type 'System.Object' is not defined or imported
Predefined type 'System.String' is not defined or imported
Predefined type 'System.Void' is not defined or imported
'object' does not contain a constructor that takes 0 arguments
Я могу только догадываться, что это происходит из-за некоторых проблем конфигурации, но с таким количеством версий .NET и таким количеством версий VS невозможно попробоватьвсе комбинации.