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?
this
keyword for extension methods in C#. – Diffusivity