C# code builder/formatter - c#

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.

Have you looked at Microsoft.CSharp.CSharpCodeProvider?
Provides access to instances of the C# code generator and code compiler.

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.

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:
[DRAFT] A Functional Language For Generating Structured Text
Enforcing Strict Model-View Separation in Template Engines
More at http://www.stringtemplate.org/article/list
CodeProject has an article on code generation with StringTemplate as well: http://www.codeproject.com/KB/codegen/DotNetCodeGeneration.aspx

Related

MarkdownSharp & GitHub syntax for C# code

Is there a way to get MarkdownSharp (I'm using the NuGet package) to handle 'GitHub flavored Markdown (GFM)' and especially syntax highlighting of c# code, which (in GFM) is written like this:
```c#
//my code.....
```
So, if I pass Markdown formatted content to MarkDownSharp, containg a C# code block (as above) I want it to generate syntax highlighted html for that c# code. Any ideas? I know I can use the supported 4 spaces to indicate a code block, but again, I'm seeking a solution for getting it to support GitHub flavored Markdown.
I have made some light modifications to MarkdownSharp that will transform github flavored fenced code blocks
https://github.com/KyleGobel/MarkdownSharp-GithubCodeBlocks
```cs
Console.WriteLine("Fenced code blocks ftw!");
```
Would become
<pre><code class='language-cs'>
Console.WriteLine("Fenced code blocks ftw!");
</code></pre>
It handles the cases I needed to use, there are probably lots of edge cases though, feel free to fork/change/modify/pull request. Markdown sharp has plenty of comments and is all only one file, so it's not too bad to modify.
Here's the result: https://github.com/danielwertheim/kiwi/wiki/Use-with-Asp.Net-MVC
//D
As one can read in this post, GitHub relies on RedCarpet to render Markdown syntax.
However, Vicent Marti (Sundown (ex-Upskirt) and RedCarpet maintainer) states that the syntax highlighting is specifically handled by Pygments, a python library.
Back to your concern, I can think of several options to benefit from syntax highlighting from C#:
Try and build a compiled managed version of Pygments source code thanks to IronPython ("IronPython’s Hosting APIs can be used to compile Python scripts into DLLs, console executables, or Windows executables.")
Port Pygment to C#
Use a different syntax highlighting product (for instance, ColorCode which is used by Codeplex...)
Then either:
Fork MarkDownSharp to make it accept plug-ins
Similarly to what GitHub does, use the managed syntax highlighting product and post process the Html generated by MarkDownSharp
By the way, as a MarkDown alternative, you might want to consider Moonshine, a managed wrapper on top of Sundown which is said to be "at least 20x faster than MarkdownSharp when run against MarkdownSharp's own benchmark app."

CppCodeGenerator parse managed C++

I am looking to parse managed C++ files into a CodeDOM tree (or any other c# representation, for that matter). I see that CppCodeGenerator has been removed in .NET4, and it does not provide a Parse(string) implementation. Any ideas?
Thanks
Can't help with CodeDom; according to other posters there's no help for it anyway.
If you want robust, accurate parsers for C#, VB.net, VC++ and managed VC++ (and many other languages) you might consider our DMS Software Reengineering Toolkit and its family of language front-ends.
Using a front-end, DMS parses source into a detailed AST enabling further analysis, transformation, and generation of valid source code from modified ASTs. No, you can't manipulate these trees using C# calls; you have to do that from inside DMS, which offers a complete ecosystem for these tasks. But you can manipulate them from inside DMS in virtually arbitary ways.

How to parse simple statement into CodeDom object

I need to parse a simple statement (essentially a chain of function calls on some object) represented as a string variable into a CodeDom object (probably a subclass of CodeStatement). I would also like to provide some default imports of namespaces to be able to use less verbose statements.
I have looked around SO and the Internet to find some suggestions but I'm quite confused about what is and isn't possible and what is the simplest way to do it. For example this question seems to be almost what I want, unfortunately I can't use the solution as the CodeSnippetStatement seems not to be supported by the execution engine that I use (the WF rules engine).
Any suggestions that could help me / point me into the right direction ?
There is no library or function to parse C# code into CodeDOM objects as part of the standard .NET libraries. The CodeDOM libraries have some methods that seem to be designed for this, but none of them are actually implemented. As far as I know, there is some implementation available in Visual Studio (used e.g. by designers), but that is only internal.
CodeSnippetStatement is a CodeDOM node that allows you to place any string into the generated code. If you want to create CodeDOM tree just to generate C# source code, than this is usually fine (the source code generator just prints the string to the output). If the WF engine needs to understand the code in your string (and not just generate source code and compile it), than CodeSnippetStatement won't work.
However, there are 3rd party tools that can be used for parsing C# source code. In one project I worked on, we used NRefactory library (which is used in SharpDevelop) and it worked quite well. It gives you some tree (AST) representing the parsed code and I'm afraid you'll need to convert this to the corresponding CodeDOM tree yourself.
I have found a library implementation here that seems to cover pretty much everything I need for my purposes. I don't know if it's robust enough to be used in business scenarios, but for my unit tests it's pretty much all I can ask for.

Using reflection for code gen?

I'm writing a console tool to generate some C# code for objects in a class library. The best/easiest way I can actual generate the code is to use reflection after the library has been built. It works great, but this seems like a haphazard approch at best. Since the generated code will be compiled with the library, after making a change I'll need to build the solution twice to get the final result, etc. Some of these issues could be mitigated with a build script, but it still feels like a bit too much of a hack to me.
My question is, are there any high-level best practices for this sort of thing?
Its pretty unclear what you are doing, but what does seem clear is that you have some base line code, and based on some its properties, you want to generate more code.
So the key issue here are, given the base line code, how do you extract interesting properties, and how do you generate code from those properties?
Reflection is a way to extract properties of code running (well, at least loaded) into the same execution enviroment as the reflection user code. The problem with reflection is it only provides a very limited set of properties, typically lists of classes, methods, or perhaps names of arguments. IF all the code generation you want to do can be done with just that, well, then reflection seems just fine. But if you want more detailed properties about the code, reflection won't cut it.
In fact, the only artifact from which truly arbitrary code properties can be extracted is the the source code as a character string (how else could you answer, is the number of characters between the add operator and T in middle of the variable name is a prime number?). As a practical matter, properties you can get from character strings are generally not very helpful (see the example I just gave :).
The compiler guys have spent the last 60 years figuring out how to extract interesting program properties and you'd be a complete idiot to ignore what they've learned in that half century.
They have settled on a number of relatively standard "compiler data structures": abstract syntax trees (ASTs), symbol tables (STs), control flow graphs (CFGs), data flow facts (DFFs), program triples, ponter analyses, etc.
If you want to analyze or generate code, your best bet is to process it first into such standard compiler data structures and then do the job. If you have ASTs, you can answer all kinds of question about what operators and operands are used. If you have STs, you can answer questions about where-defined, where-visible and what-type. If you have CFGs, you can answer questions about "this-before-that", "what conditions does statement X depend upon". If you have DFFs, you can determine which assignments affect the actions at a point in the code. Reflection will never provide this IMHO, because it will always be limited to what the runtime system developers are willing to keep around when running a program. (Maybe someday they'll keep all the compiler data structures around, but then it won't be reflection; it will just finally be compiler support).
Now, after you have determined the properties of interest, what do you do for code generation? Here the compiler guys have been so focused on generation of machine code that they don't offer standard answers. The guys that do are the program transformation community (http://en.wikipedia.org/wiki/Program_transformation). Here the idea is to keep at least one representation of your program as ASTs, and to provide special support for matching source code syntax (by constructing pattern-match ASTs from the code fragments of interest), and provide "rewrite" rules that say in effect, "when you see this pattern, then replace it by that pattern under this condition".
By connecting the condition to various property-extracting mechanisms from the compiler guys, you get relatively easy way to say what you want backed up by that 50 years of experience. Such program transformation systems have the ability to read in source code,
carry out analysis and transformations, and generally to regenerate code after transformation.
For your code generation task, you'd read in the base line code into ASTs, apply analyses to determine properties of interesting, use transformations to generate new ASTs, and then spit out the answer.
For such a system to be useful, it also has to be able to parse and prettyprint a wide variety of source code langauges, so that folks other than C# lovers can also have the benefits of code analysis and generation.
These ideas are all reified in the
DMS Software Reengineering Toolkit. DMS handles C, C++, C#, Java, COBOL, JavaScript, PHP, Verilog, ... and a lot of other langauges.
(I'm the architect of DMS, so I have a rather biased view. YMMV).
Have you considered using T4 templates for performing the code generation? It looks like it's getting much more publicity and attention now and more support in VS2010.
This tutorial seems database centric but it may give you some pointers: http://www.olegsych.com/2008/09/t4-tutorial-creatating-your-first-code-generator/ in addition there was a recent Hanselminutes on T4 here: http://www.hanselminutes.com/default.aspx?showID=170.
Edit: Another great place is the T4 tag here on StackOverflow: https://stackoverflow.com/questions/tagged/t4
EDIT: (By asker, new developments)
As of VS2012, T4 now supports reflection over an active project in a single step. This means you can make a change to your code, and the compiled output of the T4 template will reflect the newest version, without requiring you to perform a second reflect/build step. With this capability, I'm marking this as the accepted answer.
You may wish to use CodeDom, so that you only have to build once.
First, I would read this CodeProject article to make sure there are not language-specific features you'd be unable to support without using Reflection.
From what I understand, you could use something like Common Compiler Infrastructure (http://ccimetadata.codeplex.com/) to programatically analyze your existing c# source.
This looks pretty involved to me though, and CCI apparently only has full support for C# language spec 2. A better strategy may be to streamline your existing method instead.
I'm not sure of the best way to do this, but you could do this
As a post-build step on your base dll, run the code generator
As another post-build step, run csc or msbuild to build the generated dll
Other things which depend on the generated dll will also need to depend on the base dll, so the build order remains correct

Code generation tool targeting C++ and C#

I have a set of applications that are being built using a combination of C# and C++. We have a set of shared objects between the two languages, and rather than define each one separately in each language, I would prefer to use a code generation tool. Ideally such a tool would be FOSS, although that's not an absolute requirement. The objects themselves are relatively simple, although there is inheritance from baseclasses, implementation of interfaces, containment of other object types, and collections of other object types.
The C++ target environment is Visual C++ 2008.
Does anyone have any recommendations for a tool that can handle this kind of task?
Example code:
public class Tax
{
private static Dictionary<string, double> _TaxRates;
public Dictionary<string,double> TaxRates { get { return _TaxRates; } }
}
For any code generation problem I'd take a good look at T4 (the text templating functionality that appeared in VS.NET 2008).
A good place to start with T4 is...
http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx
You would need different templates for C++ and C#, and would drive your code generation from some other metadata.
The alternative is to use the CodeDom API. This has two CodeDomProviders (CppCodeProvider and CSharpCodeProvider) that can target each language.
For more information:
http://msdn.microsoft.com/en-us/library/system.codedom.compiler.aspx
There's a related links on SO:
T4 vs CodeDom vs Oslo
Is it possible to dynamically compile and execute C# code fragments?
cog is a code-generation tool that lets you embed code-generation python code in your C++/C# files. Its advantage is that it's really easy to use, especially if you already know python.
Google Protobuf might one of the best solution available for free.
See the language page.
You can also investigate CodeSmith. You can write templates in asp.net like language to generate any sort of code.
The .Net classes for emitting source code, for example the GenerateCodeFromCompileUnit method of the CodeDomProvider base class, can be used to emit source code in various languages from a single definition: for example, there's a CppCodeProvider as well as a CSharpCodeProvider. It may only be managed C++, but you did say "relatively simple".
The way I've done this multiple times is to define my own LL language and parse it, and have it generate class definitions in either language, OR write the class definitions in one target language, and then parse them to generate the other language.
I can highly recommend using Ruby as a code generator tool.
It is easy to create internal DSL's in Ruby and to generate code from it.
I use Ruby every day to generate both C++ and C# code. It has dramatically reduced the time I need to write the boring/tedious part of the application.

Categories

Resources