.net.net-corescripting-languagedynamic-language-runtime

DLR: Put statement and declaration objects to CodeCompileUnit or CodeCompileUnit-like objects


IronRuby and IronPython have custom built-ins, standard modules and you can write code straight out of hand(Directly using statements and declarations in the code), instead of using namespaces and classes like in C# and VB.NET.

I am trying to use CodeCompileUnit, but I can only find Namespaces, AssemblyCustomAttributes, StartDirectives, EndDirectives and ReferencedAssemblies properties. No other stuff, not even classes(you can put classes without namespaces in C# and VB.NET).

I am trying to find a way to be able to add stuff like variable declaration, methods and expressions to the CodeCompileUnit instead of namespaces and directives only.

How can I achieve this?


Solution

  • System.CodeDOM is a code generation system that has been built to target language features that are common to many .NET languages, in particular C# and VB.NET. It has not been designed to support specialities of any .NET language.

    Even though you may be allowed to put statements as top-level entities in IronPython or IronRuby, you are not allowed to do so in C# or VB.NET and therefore, this is not supported by System.CodeDOM.

    Note that also the development of System.CodeDOM essentially stopped after .NET 2.0. Therefore, this code model does not support more recent syntax features of C# or VB.NET as well, such as expressions-syntax, lambda expressions or async/await. From my impression, System.CodeDOM was primarily invented to make things like the WinForms designer possible. In applications like this, it is not really important to have all language features of all languages covered, but only to have the commonalities covered such that you can generate the same code in multiple languages simulataneously. Because all .NET languages must support the CLI, the commonalities also include things like events that are somewhat specific (e.g. Java does not know them).

    Edit: And therefore, do not expect that System.CodeDOM covers all language features of all languages, expect it only to support enough of all languages to generate code for it.