GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - xafero/Coaster: A C# Parser library that allows easy parsing and formatting of C# source files

BranchesTags
Open more actions menu

Latest commit

 

History

61 Commits
61 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src
src
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 

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:

  • dotnet/roslyn

License

Everything is licensed according to this.

About

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

Resources

Readme
AGPL-3.0 license
Activity

Stars

30 stars

Watchers

1 watching

Forks

2 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.