Coaster 0.1.7

dotnet add package Coaster --version 0.1.7                
NuGet\Install-Package Coaster -Version 0.1.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Coaster" Version="0.1.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Coaster --version 0.1.7                
#r "nuget: Coaster, 0.1.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Coaster as a Cake Addin
#addin nuget:?package=Coaster&version=0.1.7

// Install Coaster as a Cake Tool
#tool nuget:?package=Coaster&version=0.1.7                

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.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.7 146 4/10/2024
0.1.6 97 4/10/2024
0.1.4 121 4/4/2024
0.1.3 115 4/4/2024
0.1.2 114 4/4/2024
0.1.1 113 4/4/2024
0.1.0 110 4/4/2024