如何从本地放置的 DLL 添加自定义 roslyn 分析器?
How to add custom roslyn analyzer from locally placed DLL?
我创建了一个 Roslyn Analyzer 项目,它生成了一个 nuget 包和它的 DLL。我想在独立的代码分析项目中使用该 DLL。我怎样才能做到这一点?例如我有以下代码:
MSBuildLocator.RegisterDefaults();
var filePath = @"C:\Users\user\repos\ConsoleApp\ConsoleApp.sln";
var msbws = MSBuildWorkspace.Create();
var soln = await msbws.OpenSolutionAsync(filePath);
var errors = new List<Diagnostic>();
foreach (var proj in soln.Projects)
{
var analyzer = //Here i want to load analyzer from DLL present locally.
var compilation = await proj.GetCompilationAsync();
var compWithAnalyzer = compilation.WithAnalyzers(analyzer.GetAnalyzersForAllLanguages());
var res = compWithAnalyzer.GetAllDiagnosticsAsync().Result;
errors.AddRange(res.Where(r => r.Severity == DiagnosticSeverity.Error).ToList());
}
我试过关注
var analyzer = new AnalyzerFileReference("Path to DLL", new AnalyzerAssemblyLoader());
但是这里 AnalyzerAssemblyLoader 显示错误,因为它无法达到其保护级别(class 是内部的)。
如果我们可以做到这一点,请建议我。
.WithAnalyzers()
选项将允许您传递分析器的实例。如果您在本地引用 DLL,您可以像创建任何其他对象一样创建分析器并将其传递给编译。
var analyzer = new MyAnalyzer();
var compilation = await proj.GetCompilationAsync();
var compWithAnalyzer = compilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(analyzer));
如果您不引用程序集,但想在运行时加载它,您可以使用通常的 System.Reflection
方法来获取分析器的实例:
var assembly = Assembly.LoadFrom(@"<path to assembly>.dll");
var analyzers = assembly.GetTypes()
.Where(t => t.GetCustomAttribute<DiagnosticAnalyzerAttribute>() is object)
.Select(t => (DiagnosticAnalyzer) Activator.CreateInstance(t))
.ToArray();
compWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzers));
我创建了一个 Roslyn Analyzer 项目,它生成了一个 nuget 包和它的 DLL。我想在独立的代码分析项目中使用该 DLL。我怎样才能做到这一点?例如我有以下代码:
MSBuildLocator.RegisterDefaults();
var filePath = @"C:\Users\user\repos\ConsoleApp\ConsoleApp.sln";
var msbws = MSBuildWorkspace.Create();
var soln = await msbws.OpenSolutionAsync(filePath);
var errors = new List<Diagnostic>();
foreach (var proj in soln.Projects)
{
var analyzer = //Here i want to load analyzer from DLL present locally.
var compilation = await proj.GetCompilationAsync();
var compWithAnalyzer = compilation.WithAnalyzers(analyzer.GetAnalyzersForAllLanguages());
var res = compWithAnalyzer.GetAllDiagnosticsAsync().Result;
errors.AddRange(res.Where(r => r.Severity == DiagnosticSeverity.Error).ToList());
}
我试过关注
var analyzer = new AnalyzerFileReference("Path to DLL", new AnalyzerAssemblyLoader());
但是这里 AnalyzerAssemblyLoader 显示错误,因为它无法达到其保护级别(class 是内部的)。
如果我们可以做到这一点,请建议我。
.WithAnalyzers()
选项将允许您传递分析器的实例。如果您在本地引用 DLL,您可以像创建任何其他对象一样创建分析器并将其传递给编译。
var analyzer = new MyAnalyzer();
var compilation = await proj.GetCompilationAsync();
var compWithAnalyzer = compilation.WithAnalyzers(ImmutableArray.Create<DiagnosticAnalyzer>(analyzer));
如果您不引用程序集,但想在运行时加载它,您可以使用通常的 System.Reflection
方法来获取分析器的实例:
var assembly = Assembly.LoadFrom(@"<path to assembly>.dll");
var analyzers = assembly.GetTypes()
.Where(t => t.GetCustomAttribute<DiagnosticAnalyzerAttribute>() is object)
.Select(t => (DiagnosticAnalyzer) Activator.CreateInstance(t))
.ToArray();
compWithAnalyzers = compilation.WithAnalyzers(ImmutableArray.Create(analyzers));