Skip to content
AlexMacocianPublic

About

C# Syntax builders following builder pattern

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Sybil

C# Syntax builders following builder pattern

Usage

Sybil supports building of compilation units, namespaces, classes, interfaces, enums, constructors, parameters, properties, fields and methods. Example of creating a namespace with one class with one constructor and base class, multiple fields, properties and methods:

var namespaceSyntax = SyntaxBuilder.CreateNamespace("TestNamespace")
        .WithAttribute(SyntaxBuilder.CreateAttribute("InternalsVisibleTo")
            .WithArgument("FlowTests"))
        .WithUsing("System")
        .WithClass(
            SyntaxBuilder.CreateClass("TestClass")
            .WithBaseClass("BaseTestClass")
            .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute")
                .WithArgument("SomeIntProperty", 1)
                .WithArgument("SomeStringProperty", "Hello")
                .WithArgument("SomeEnumProperty", MyEnum.Value)
                .WithArgument("SomeTypeProperty", typeof(string))
                .WithNullArgument("SomeNullProperty"))
            .WithModifiers("public sealed")
            .WithConstructor(
                SyntaxBuilder.CreateConstructor("TestClass")
                .WithBase(
                    SyntaxBuilder.CreateBaseConstructor()
                    .WithArgument("fieldString"))
                .WithAttribute(
                    SyntaxBuilder.CreateAttribute("SomeAttribute5")
                    .WithNullArgument())
                .WithParameter("string", "fieldString")
                .WithBody("this.fieldString = fieldString ?? throw new ArgumentNullException();"))
            .WithField(
                SyntaxBuilder.CreateField("string", "fieldString")
                .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute2"))
                .WithModifier("private"))
            .WithProperty(
                SyntaxBuilder.CreateProperty("string", "PropertyString")
                .WithModifier("public")
                .WithAccessor(
                    SyntaxBuilder.CreateSetter()
                    .WithModifier("private"))
                .WithAccessor(
                    SyntaxBuilder.CreateGetter())
                .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute3")
                    .WithArgument(typeof(string))))
            .WithMethod(
                SyntaxBuilder.CreateMethod("string", "GetFieldString")
                .WithModifier("public")
                .WithAttribute(SyntaxBuilder.CreateAttribute("SomeAttribute4")
                    .WithArgument(0.5f))
                .WithBody("return this.fieldString;")))
        .Build();

        var namespaceString = namespaceSyntax.ToFullString();

This generates the following syntax:

[InternalsVisibleTo(""FlowTests"")]
namespace TestNamespace
{
    using System;

    [SomeAttribute(SomeIntProperty = 1, SomeStringProperty = ""Hello"", SomeEnumProperty = MyEnum.Value, SomeTypeProperty = typeof(String), SomeNullProperty = null)]
    public sealed class TestClass : BaseTestClass
    {
        [SomeAttribute5(null)]
        TestClass(string fieldString) : base(fieldString)
        {
            this.fieldString = fieldString ?? throw new ArgumentNullException();
        }

        [SomeAttribute2]
        private string fieldString;
        [SomeAttribute3(typeof(String))]
        public string PropertyString { private set; get; }

        [SomeAttribute4(0.5F)]
        public string GetFieldString()
        {
            return this.fieldString;
        }
    }
}

Builders

Builder Created with Notes
CompilationUnitBuilder SyntaxBuilder.CreateCompilationUnit() WithHeaderComment and WithNullableContext emit a leading comment and #nullable directive
NamespaceBuilder CreateNamespace / CreateFileScopedNamespace Holds classes, structs, interfaces and enums; file scoped namespaces are followed by a blank line
ClassBuilder CreateClass
StructBuilder CreateStruct Like ClassBuilder without a base class; modifiers such as readonly, ref, partial
InterfaceBuilder CreateInterface
EnumBuilder CreateEnum Members with optional values, base type, attributes
ConstructorBuilder CreateConstructor
MethodBuilder CreateMethod
ParameterBuilder CreateParameter Attributes, modifiers (params, this, ...) and default values; pass to WithParameter(ParameterBuilder)
PropertyBuilder / AccessorBuilder CreateProperty, CreateGetter, CreateSetter
FieldBuilder CreateField
AttributeBuilder CreateAttribute

Bodies

WithBody(string) parses the whole text as one block, so statements may span multiple lines (object initializers, loops, multi-line verbatim strings). WithStatements(IEnumerable<StatementSyntax>) accepts statements built with Roslyn's SyntaxFactory. Output is formatted with Roslyn's NormalizeWhitespace. Empty bodies: WithStatements([]) (WithBody rejects empty text).

About

C# Syntax builders following builder pattern

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages