什么是建立从一开始就的IDocument最有效的方法 [英] What's the most efficient way to build up a IDocument from the very start

查看:201
本文介绍了什么是建立从一开始就的IDocument最有效的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个脚印使用下面的类作为一个具体的例子一个新的IDocument对象的一步。你可以从任何你喜欢的对象,并使用任何中间对象,你喜欢,只要得到的对象是重新presents完整的类在最后一个的IDocument。

I would like to build up a new IDocument object step by step using the following class as a specific example. You can start with any object you like and use any intermediate objects you like as long as the resulting object is an IDocument which represents the complete class at the end.

步骤#1:添加一个名为myNameSpace对象新的命名空间。 打印出当前的对象应该是这样的,在这一点上:

Step #1: Add a new namespace called MyNamespace. Printing out the current object should look like this at this point:

namespace MyNamespace
{
}

第2步:添加一个新的类名为MyClass的这个命名空间。 打印出当前的对象应该是这样的,在这一点上:

Step #2: Add a new class to this namespace called MyClass. Printing out the current object should look like this at this point:

namespace MyNamespace
{
    public class MyClass
    {
    }
}

第三步:添加新的方法,这个类叫的MyMethod。 打印出当前的对象应该是这样的,在这一点上:

Step #3: Add a new method to this class called MyMethod. Printing out the current object should look like this at this point:

namespace MyNamespace
    {
        public class MyClass
        {
            public void MyMethod()
            {
            }
        }
    }

我有这样的问题是似乎有一个极大的方式理论上可以去这个问题,或者至少是不正确的结论,你可以去这个问题。无尽的方法和构造各种像WithChanges,UpdateDocument,对各种语法对象的方法,ParseCompilationUnit等。

The problem I am having with this is there seems to be a gazillion ways you could theoretically go about this, or at least incorrectly conclude you could go about this. Endless methods and constructors in all kinds of different objects like WithChanges, UpdateDocument, methods on the various Syntax objects, ParseCompilationUnit, etc.

基本上,我想在每一步,我可以打印到控制台,例如,在一个行创建这件事,而不是一个大的语句,不同的对象建立这个在增量方式。我已阅读所有附带的CTP多次的6月发行的文件,正如我刚才所说,我迷失在各种构造函数和方法的无休止的组合。另外,我感兴趣的方式,采用性能考虑在内也是如此。

Basically, I want to build this up in an incremental fashion with a distinct object at each step that I could print out to the console for example, not one big statement that creates this whole thing in one line. I have read all the documentation that comes with the June release of the CTP several times, and as I have mentioned I am lost in the seemingly endless combinations of various constructors and methods. Also, I am interested in a way that takes performance into account as well.

推荐答案

要建立起来的一切零碎的,你认为我会写一些像下面的code。我也建议你看一看的ImplementINotifyPropertyChanged样品,因为它的语法结构的公平大写金额和再创作。请注意,正如你提到的,有各种各样的方式,你可以做到这一点。这是因为该API的设计支持方案,例如编辑器,让您可以通过应用文本更改为用户输入的每个按键以及建立这个。其中API是正确的取决于你正在努力实现的目标。

To build everything up piecemeal as you suggest I would write something like the code below. I would also encourage you to take a look at the ImplementINotifyPropertyChanged sample, as it does a fair amout of syntax construction and re-writing. Note, that as you suggest, there are a variety of ways that you could do this. That's because the API is designed to support scenarios such as editors, so you could build this by applying text changes for each keystroke of a user typing as well. Which API is the right one depends on what you are trying to achieve.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;
using Roslyn.Services.CSharp;

class Program
{
    static void Main(string[] args)
    {
        // Create the solution with an empty document
        ProjectId projectId;
        DocumentId documentId;
        var solution = Solution.Create(SolutionId.CreateNewId())
            .AddProject("MyProject", "MyProject", LanguageNames.CSharp, out projectId)
            .AddDocument(projectId, @"C:\file.cs", string.Empty, out documentId);

        var document = solution.GetDocument(documentId);
        var root = (CompilationUnitSyntax)document.GetSyntaxRoot();

        // Add the namespace
        var namespaceAnnotation = new SyntaxAnnotation();
        root = root.WithMembers(
            Syntax.NamespaceDeclaration(
                Syntax.ParseName("MyNamespace"))
                    .NormalizeWhitespace()
                    .WithAdditionalAnnotations(namespaceAnnotation));
        document = document.UpdateSyntaxRoot(root);

        Console.WriteLine("-------------------");
        Console.WriteLine("With Namespace");
        Console.WriteLine(document.GetText().GetText());

        // Find our namespace, add a class to it, and update the document
        var namespaceNode = (NamespaceDeclarationSyntax)root
            .GetAnnotatedNodesAndTokens(namespaceAnnotation)
            .Single()
            .AsNode();

        var classAnnotation = new SyntaxAnnotation();
        var newNamespaceNode = namespaceNode
            .WithMembers(
                Syntax.List<MemberDeclarationSyntax>(
                    Syntax.ClassDeclaration("MyClass")
                        .WithAdditionalAnnotations(classAnnotation)));
        root = root.ReplaceNode(namespaceNode, newNamespaceNode).NormalizeWhitespace();
        document = document.UpdateSyntaxRoot(root);

        Console.WriteLine("-------------------");
        Console.WriteLine("With Class");
        Console.WriteLine(document.GetText().GetText());

        // Find the class, add a method to it and update the document
        var classNode = (ClassDeclarationSyntax)root
            .GetAnnotatedNodesAndTokens(classAnnotation)
            .Single()
            .AsNode();
        var newClassNode = classNode
            .WithMembers(
                Syntax.List<MemberDeclarationSyntax>(
                    Syntax.MethodDeclaration(
                        Syntax.ParseTypeName("void"),
                        "MyMethod")
                        .WithBody(
                            Syntax.Block())));
        root = root.ReplaceNode(classNode, newClassNode).NormalizeWhitespace();
        document = document.UpdateSyntaxRoot(root);

        Console.WriteLine("-------------------");
        Console.WriteLine("With Method");
        Console.WriteLine(document.GetText().GetText());
    }
}

这篇关于什么是建立从一开始就的IDocument最有效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆