Closed ufcpp closed 4 years ago
using Generator1;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Immutable;
using System.IO;
class Program
{
static void Main()
{
var source = @"class Program { static void Main() { } }";
var compilation = Compile(source);
foreach (var diag in compilation.GetDiagnostics())
{
Console.WriteLine(diag);
}
}
private static Compilation Compile(string source)
{
var dotnetCoreDirectory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
var compilation = CSharpCompilation.Create("test",
syntaxTrees: new[] { SyntaxFactory.ParseSyntaxTree(source) },
references: new[]
{
AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference(),
MetadataReference.CreateFromFile(Path.Combine(dotnetCoreDirectory, "netstandard.dll")),
MetadataReference.CreateFromFile(Path.Combine(dotnetCoreDirectory, "System.Runtime.dll")),
});
// ここから
var opt = new CSharpParseOptions(kind: SourceCodeKind.Regular, documentationMode: DocumentationMode.Parse);
var driver = new CSharpGeneratorDriver(opt, ImmutableArray.Create<ISourceGenerator>(new Class1()), ImmutableArray<AdditionalText>.Empty);
driver.RunFullGeneration(compilation, out var resultCompilation, out _);
// ここまで
return resultCompilation;
}
}
一応これでステップインできそう。
そしてアナリティクスを見てると…
今でも https://youtu.be/W5u0GvCPx7Y こいつの再生数100単位であるんか…
https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/
.NET 5 Preview 3で入ってたらしいもの、C# チームの人がブログで説明してた。
https://stackoverflow.com/questions/55244265/use-roslyn-code-analyzer-in-same-solution