Skip to content
/ Coaster Public

A C# Parser library that allows easy parsing and formatting of C# source files

License

Notifications You must be signed in to change notification settings

xafero/Coaster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coaster

Build NuGet Downloads

The project Coaster is a library that allows easy parsing and formatting of C# source files. Coaster introduces a nice interface to manipulate C# source files, like adding fields, methods, attributes and so on.

Installation

dotnet add package Coaster

Usage

C# Parser API

Example:

Coast.Parse("public class HelloWorld {}");

C# Source Code Generation API

Coaster provides a nice API to generate C# classes. Here is an example:

var unit = new CUnit { Usings = { "System.Linq", "System", "System.IO" }, Members =
    {
        new CNamespace { Name = "Example", Members =
            {
                new CClass { Name = "Person", Members =
                    {
                        new CProperty { Type = "int", Name = "Id" },
                        new CProperty { Type = "string", Name = "FirstName" },
                        new CProperty { Type = "string", Name = "LastName" }
                    }
                }
            }
        }
    }
};
Console.WriteLine(unit.ToText());

This will produce:

using System;
using System.IO;
using System.Linq;

namespace Example
{
    public class Person 
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

C# Source Code Modification API

Of course, it is possible to mix both approaches (parser and writer) to modify C# code programmatically:

var unit = Coast.Parse("public class SomeClass {}");
var clazz = unit.Members.Cast<CClass>().Single();

clazz.Members.Add(new CMethod { Name = "Main" });

Console.WriteLine(unit.ToText());

Formatting the C# Source Code

Coaster formats the C# Source Code by calling the Format() method:

var humanCode = "public class MyClass{ private string field;}";
var formattedCode = Coast.Format(humanCode);
Console.WriteLine(formattedCode);

Executing the C# Source Code

Coaster provides you with a delegate to the C# Source Code:

var func = Compiler.CreateDelegate<Func<string, string>>(unit.ToText());
Console.WriteLine(func("Michael Che"));
Console.WriteLine(func("Fritz Wepp"));

Building from sources

Just run dotnet pack to build the sources.

Background info

This project uses the syntax tree parsing and writing of:

License

Everything is licensed according to this.

About

A C# Parser library that allows easy parsing and formatting of C# source files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages