ufcpp-live / UfcppLiveAgenda

@ufcpp live streaming agenda
MIT License
24 stars 2 forks source link

【C# 9.0/.NET 5候補】Source Generator を試してみる回 #5

Closed ufcpp closed 4 years ago

ufcpp commented 4 years ago

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

ufcpp commented 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;
    }
}

一応これでステップインできそう。

ufcpp-live commented 4 years ago

https://youtu.be/aZENYVSEpIY

ufcpp-live commented 4 years ago

そしてアナリティクスを見てると…

image

今でも https://youtu.be/W5u0GvCPx7Y こいつの再生数100単位であるんか…