Domain Specific Language for C# code generationCsBuilder is a strong example of the power that C# offers in creating Domain Specific Languages. It aims to generate C# code through a fluent interface that mimics real C# code.
A collection of classes modeling C# code is behind a facade of methods heavily relying in lambda expressions to achieve the feeling of coding.
using(ICodeWriter codeWriter = new CodeWriter(new StreamWriter(File.Create("sum.cs"))))
{
Cs.Class("Sum", c =>
{
c.Method("DoSum", m =>
{
var a = Cs.Var("a");
var b = Cs.Var("b");
m.Return(a + b);
}).OfType(CsType.Int).Param("a", CsType.Int).Param("b", CsType.Int);
})
.Render(codeWriter);
}