Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is There an Official Replacement for CodeDom?

I am looking into the System.CodeDom namespace for language-independent (at least within certain bounds) source code generation, and I have found some information discouraging the use of CodeDom.

I think some of the omissions described in this early blogpost have been fixed by now, and the fact that CodeDom does not seem to provide a way to create a switch statement still allows for - less performant? - workarounds without uglifying the public interface of the generated types. The same applies to automatic C# properties and collection initializers.

However, other omissions cannot really be worked around, such as the inability to create finalizers, the impossibility to declare extension methods, or the lacking direct support of generic reference type constraints.

Note that the suggested solutions with CodeSnippetTypeMember or injecting literal source code snippets by any other means are not satisfying as they are not language-independent - thereby removing the whole point of using CodeDom rather than String.Format with literal code snippets.

Lastly, it is even suggested in this SO question that 'CodeDom is a failure and that Expression Trees (or rather "Statement" Trees) are the way forward' - though without any explanation how to actually obtain any source code from an expression tree (beside the restriction that classes cannot be declared with expression trees.

Is CodeDom still the method of choice to have source code generated, or does the current BCL offer any obscure replacement with a name that I did not think of?

like image 651
O. R. Mapper Avatar asked Feb 10 '13 11:02

O. R. Mapper


2 Answers

I think CodeDom is still the best solution in the BCL today, but: The Roslyn project is far along and has already launched several CTPs. The goal is to make the compiler available as a service, and it will enable code generation and code inspection scenarios with a simple API.

Take a look at it, if you can use pre-release bits for your project: Roslyn CTP. Here is a related (albeit outdated, still some good info) StackOverflow question: Microsoft Roslyn vs. CodeDom. And finally, an article that talks about using Roslyn for code generation: Code Generation in .NET with the Roslyn CTP

like image 131
driis Avatar answered Sep 18 '22 03:09

driis


No. CodeDom is useful to generate code to execute. Its ability to also generate text was merely an accidental by-product, required because compilers need text. If you really care about the text then there are lots of reasons to dislike CodeDom and nothing in the framework to help you.

Other solutions are similarly focused on generating executable code. Reflection.Emit generates IL, the universal language in .NET, but offers no simple way to decompile although any decent decompiler (ILSpy, Reflector, etc) can certainly help. Linq.Expressions is pure executable code and not generally useful to generate complete programs. Roslyn is heavily biased towards already having the text.

Probably the best way forward is to deprecate your requirement for having the text in a specific language. All .NET compilers have the same kind of output, they all compile to IL. Which makes limiting your choices to one language a viable approach. It isn't otherwise clear from your question whether that's a reasonable limitation at all. It does appear to be a common choice, I can't think of a project that ever tried to solve the CodeDom restrictions. The kind of projects available at codeplex.com that target CodeDom instead try to minimize the pain of the verbosity required in code that uses CodeDom.

like image 41
Hans Passant Avatar answered Sep 20 '22 03:09

Hans Passant