簡體   English   中英

使用Roslyn編譯語法樹

[英]Compiling a Syntax Tree using Roslyn

我正在嘗試使用Roslyn生成和編譯包含get / set屬性的簡單對象的運行時庫。

但是,由於某種原因,編譯程序集失敗,添加Linq名稱空間時出錯(錯誤CS0246:找不到類型或名稱空間名稱'System.Linq'(您是否缺少using指令或程序集引用?)}) 。

我曾嘗試以多種方式操作生成的樹並編譯每個,但仍然編譯失敗。

編譯成功的唯一方法是將樹解析為字符串,然后解析回語法樹然后編譯。

以下代碼執行以下操作:

  1. 構建一個包含編譯單元,usings,namespace,class和property的簡單語法樹。
  2. 嘗試編譯樹(失敗)
  3. 使用C#6選項生成新的語法樹並編譯(失敗)
  4. 格式化語法樹並編譯(失敗)
  5. 將樹序列化為字符串,然后使用SyntaxFactory.ParseSyntaxTree並編譯生成的樹(成功)

編碼:

    private static readonly CSharpCompilationOptions DefaultCompilationOptions =
        new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                .WithOverflowChecks(true)
                .WithPlatform(Platform.X86)
                .WithOptimizationLevel(OptimizationLevel.Release)
                .WithUsings(DefaultNamespaces);
    private static readonly IEnumerable<string> DefaultNamespaces =
        new[]
        {
                    "System",
                    "System.IO",
                    "System.Net",
                    "System.Linq",
                    "System.Text",
                    "System.Text.RegularExpressions"
        };

    private static readonly IEnumerable<MetadataReference> DefaultReferences =
        new[]
        {
                    MetadataReference.CreateFromFile(typeof (object).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof (System.Linq.Enumerable).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof (System.GenericUriParser).Assembly.Location),
                    MetadataReference.CreateFromFile(typeof (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException).Assembly.Location)
        };

    static void Main(string[] args)
    {
        MakeAssembly();
        Console.ReadLine();
    }

    private static void MakeAssembly()
    {
        //Compilation Unit and Usings
        CompilationUnitSyntax cu = SyntaxFactory.CompilationUnit()
            .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System")),
            SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName(typeof(System.Linq.Enumerable).Namespace)))
        ;

        // NameSpace
        NamespaceDeclarationSyntax ns = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.IdentifierName("Roslyn"));

        // Class
        ClassDeclarationSyntax classNode = SyntaxFactory.ClassDeclaration("MyClass")
                        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                    ;

        // Property
        classNode= classNode.AddMembers(
                                SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName("Int32"), "MyProperty")
                                        .AddAccessorListAccessors(
                                        SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                                        SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))).
                                        AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword)));
        ns = ns.AddMembers(classNode);
        cu = cu.AddMembers(ns);

        // Try To Compile Syntax Tree root
        var root = cu.SyntaxTree.GetRoot();
        var st = root.SyntaxTree;
        var assembly = CompileAndLoad(st);

        if (assembly != null)
        {
            Console.WriteLine("Success compile syntax tree root");
            return;
        }
        else
            Console.WriteLine("failed to compile syntax tree root");

        // Try to compile new syntax tree
        var stNew = SyntaxFactory.SyntaxTree(cu, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6));
        assembly = CompileAndLoad(stNew);
        if (assembly != null)
        {
            Console.WriteLine("Success compile new syntax tree");
            return;
        }
        else
            Console.WriteLine("failed to compile new syntax tree");

        // Try to format node
        AdhocWorkspace cw = new AdhocWorkspace();
        OptionSet options = cw.Options;
        options = options.WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInMethods, false);
        options = options.WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInTypes, false);

        SyntaxNode formattedNode = Formatter.Format(cu, cw, options);
        var stFormat = SyntaxFactory.SyntaxTree(cu, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6));
        assembly = CompileAndLoad(stFormat);
        if (assembly != null)
        {
            Console.WriteLine("Success compile formatted syntax tree");
            return;
        }
        else
            Console.WriteLine("failed to compile formatted syntax tree");


        // Try to serialize and parse
        StringBuilder sb = new StringBuilder();
        using (StringWriter writer = new StringWriter(sb))
        {
            formattedNode.WriteTo(writer);
        }
        var treeAsString = sb.ToString();
        var stParsed = SyntaxFactory.ParseSyntaxTree(treeAsString);
        assembly = CompileAndLoad(stParsed);
        if (assembly != null)
        {
            Console.WriteLine("Success compile parsed syntax tree");
            return;
        }   
        else
            Console.WriteLine("failed to compile formatted syntax tree");

    }

    private static Assembly CompileAndLoad(SyntaxTree st)
    {
        var compilation
            = CSharpCompilation.Create("TestRoslyn.dll", new SyntaxTree[] { st }, null, DefaultCompilationOptions);
        compilation = compilation.WithReferences(DefaultReferences);
        using (var stream = new MemoryStream())
        {
            EmitResult result = compilation.Emit(stream);
            if (result.Success)
            {
                var assembly = Assembly.Load(stream.GetBuffer());
                return assembly;
            }
            return null;
        }
    }

羅斯林也陷入了這個陷阱。 using指令不僅表示為字符串,限定名稱的每個部分都是語法節點。 您需要像這樣創建節點

var qualifiedName= SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"),    
                                                 SyntaxFactory.IdentifierName("Linq"));     
var usingDirective = SyntaxFactory.UsingDirective(qualifedName);

我編寫了一個幫助方法來將字符串轉換為正確的語法節點。

private UsingDirectiveSyntax CreateUsingDirective(string usingName)
{
    NameSyntax qualifiedName = null;

    foreach (var identifier in usingName.Split('.'))
    {
        var name = SyntaxFactory.IdentifierName(identifier);

        if (qualifiedName != null)
        {
            qualifiedName = SyntaxFactory.QualifiedName(qualifiedName, name);
        }
        else
        {
            qualifiedName = name;
        }
    }

    return SyntaxFactory.UsingDirective(qualifiedName);
}

您可以使用SyntaxFactory.ParseName來處理解析字符串,然后為using指令構建限定名稱語法節點:

var qualifiedName = SyntaxFactory.ParseName("System.Linq");
var usingDirective = SyntaxFactory.UsingDirective(qualifiedName);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM