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