Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aybe/72bc67be136f935075d1c6a55aa7cfd1 to your computer and use it in GitHub Desktop.

Select an option

Save aybe/72bc67be136f935075d1c6a55aa7cfd1 to your computer and use it in GitHub Desktop.

Revisions

  1. @cmendible cmendible renamed this gist Aug 16, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @cmendible cmendible created this gist Aug 16, 2017.
    85 changes: 85 additions & 0 deletions Program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    using System;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.CSharp.Syntax;

    namespace Roslyn.CodeGeneration
    {
    public class Program
    {
    public static void Main(string[] args)
    {
    // Create a class
    CreateClass();

    // Wait to exit.
    Console.Read();
    }

    /// <summary>
    /// Create a class from scratch.
    /// </summary>
    static void CreateClass()
    {
    // Create CompilationUnitSyntax
    var syntaxFactory = SyntaxFactory.CompilationUnit();

    // Add System using statement: (using System)
    syntaxFactory = syntaxFactory.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")));

    // Create a namespace: (namespace CodeGenerationSample)
    var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName("CodeGenerationSample")).NormalizeWhitespace();

    // Create a class: (class Order)
    var classDeclaration = SyntaxFactory.ClassDeclaration("Order");

    // Add the public modifier: (public class Order)
    classDeclaration = classDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

    // Inherit BaseEntity<T> and implement IHaveIdentity: (public class Order : BaseEntity<T>, IHaveIdentity)
    classDeclaration = classDeclaration.AddBaseListTypes(
    SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName("BaseEntity<Order>")),
    SyntaxFactory.SimpleBaseType(SyntaxFactory.ParseTypeName("IHaveIdentity")));

    // Create a string variable: (bool canceled;)
    var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("bool"))
    .AddVariables(SyntaxFactory.VariableDeclarator("canceled"));

    // Create a field declaration: (private bool canceled;)
    var fieldDeclaration = SyntaxFactory.FieldDeclaration(variableDeclaration)
    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));

    // Create a Property: (public int Quantity { get; set; })
    var propertyDeclaration = SyntaxFactory.PropertyDeclaration(SyntaxFactory.ParseTypeName("int"), "Quantity")
    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
    .AddAccessorListAccessors(
    SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
    SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

    // Create a stament with the body of a method.
    var syntax = SyntaxFactory.ParseStatement("canceled = true;");

    // Create a method
    var methodDeclaration = SyntaxFactory.MethodDeclaration(SyntaxFactory.ParseTypeName("void"), "MarkAsCanceled")
    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
    .WithBody(SyntaxFactory.Block(syntax));

    // Add the field, the property and method to the class.
    classDeclaration = classDeclaration.AddMembers(fieldDeclaration, propertyDeclaration, methodDeclaration);

    // Add the class to the namespace.
    @namespace = @namespace.AddMembers(classDeclaration);

    // Add the namespace to the compilation unit.
    syntaxFactory = syntaxFactory.AddMembers(@namespace);

    // Normalize and get code as string.
    var code = syntaxFactory
    .NormalizeWhitespace()
    .ToFullString();

    // Output new code to the console.
    Console.WriteLine(code);
    }
    }
    }