gpt4 book ai didi

c# - 从一开始就构建 IDocument 的最有效方法是什么

转载 作者:行者123 更新时间:2023-12-02 05:39:48 25 4
gpt4 key购买 nike

我想使用以下类作为具体示例逐步构建一个新的 IDocument 对象。您可以从您喜欢的任何对象开始,并使用您喜欢的任何中间对象,只要生成的对象是一个 IDocument,它表示最后的完整类。

第 1 步:添加一个名为 MyNamespace 的新命名空间。此时打印出当前对象应该如下所示:

namespace MyNamespace
{
}

第 2 步:向此 namespace 添加一个名为 MyClass 的新类。此时打印出当前对象应该如下所示:

namespace MyNamespace
{
public class MyClass
{
}
}

第 3 步:向此类中添加一个名为 MyMethod 的新方法。此时打印出当前对象应该如下所示:

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

我遇到的问题是,理论上似乎有无数种方法可以解决这个问题,或者至少错误地得出结论可以解决这个问题。各种不同对象(如 WithChanges、UpdateDocument、各种 Syntax 对象的方法、ParseCompilationUnit 等)中无穷无尽的方法和构造函数。

基本上,我想以增量方式构建它,在每一步都有一个不同的对象,例如我可以打印到控制台,而不是一个大的语句在一行中创建整个东西。我已经多次阅读了 6 月发布的 CTP 附带的所有文档,正如我所提到的,我迷失在各种构造函数和方法看似无穷无尽的组合中。另外,我也对一种将性能考虑在内的方法感兴趣。

最佳答案

要按照您的建议逐步构建所有内容,我会编写类似下面的代码。我还鼓励您查看 ImplementINotifyPropertyChanged 示例,因为它进行了大量的语法构造和重写。请注意,正如您所建议的,您可以通过多种方式执行此操作。这是因为该 API 旨在支持编辑器等场景,因此您也可以通过为用户键入的每次击键应用文本更改来构建它。哪种 API 是正确的取决于您要实现的目标。

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());
}
}

关于c# - 从一开始就构建 IDocument 的最有效方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11174593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com