Giter VIP home page Giter VIP logo

coaster's Introduction

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.

coaster's People

Contributors

xafero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

coaster's Issues

This is a great idea, dynamic queries

Hello this is a great idea, would be great if we could dynamic queries based on the attributes of an expando object.

Let me explain, we get new undefined JSON structures over the wire, and we have to query them. I can easily serialize them but querying them on their member attributes is a challenge, any way you can help us get the member attributes and types into a query filter would greatly help..

For our use case, when we have the class metadata, a) we display the C# metadata attributes as a filter and names which allows the user to select the attributes as filters and filter the data, b) is there a way to construct the query based on the attributes coming back from the expando/C# class

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.