Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:44:04+00:00 2026-06-06T06:44:04+00:00

I would like to build up a new IDocument object step by step using

  • 0

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.

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

namespace MyNamespace
{
}

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
    {
    }
}

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

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:44:06+00:00Added an answer on June 6, 2026 at 6:44 am

    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());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to this field, and I would like to build a small
I would like to build PolarSSL using ./configure --host=i686-w64-mingw32 but there is no configure
I would like to build a winform business solution using SiganlR, but I am
I would like to build web browser game using php. I need graphics so
I would like to build a data frame in a loop adding a new
I need to build a new website. I would like to use Silverlight. How
Im starting to build a new app and I would like to use Backbone
I have an existing DB with which I would like to build a new
I would like to build two C++ projects in the same solution in Visual
I would like to build a a UI control just like the Yahoo Finance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.