Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JasminSAB/1db6859735163cadf42c0910e3edaa97 to your computer and use it in GitHub Desktop.
Save JasminSAB/1db6859735163cadf42c0910e3edaa97 to your computer and use it in GitHub Desktop.
Create a class with dotnet core and roslyn with using statements outside the namespace
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment