Reputation: 2850
Does anyone know of a library which helps with generating C# code? For example, if I need to generate a *.cs file containing the definition of a class, I'd like to be able to specify the class and method bodies using an object tree (similar to expression trees) and then tell the library to give me well the formatted C# code as a string.
Thanks.
Upvotes: 1
Views: 681
Reputation: 74385
I'm rather fond of Terance Parr's StringTemplate. It's at the core of the compiler building tool ANTLR — StringTemplate is responsible for for code generation, allowing ANTLR to target just about any language for its compilers.
You can download the latest C#/.Net port from http://www.stringtemplate.org/download.html
You can read about string template in these papers by Dr. Parr:
CodeProject has an article on code generation with StringTemplate as well: http://www.codeproject.com/KB/codegen/DotNetCodeGeneration.aspx
Upvotes: 0
Reputation: 1107
There are many tools for code generation and they are listed below:
Code Smith codesmithtools dot com
codegeneratorpro dot com
etc
Code generators may sound like saving a lot of time, in reality it depends on the kind of project your working on.
Look into System.CodeDom and CSharpCodeProvider.
Upvotes: 0
Reputation: 499382
Have you looked at Microsoft.CSharp.CSharpCodeProvider
?
Provides access to instances of the C# code generator and code compiler.
Upvotes: 5