2

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?

  • 1
    Asking for tutorials is specifically off-topic for Stack Overflow (see the [help/on-topic]). – Heretic Monkey May 07 '19 at 19:59
  • You didn't get far in [the FM](https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/using-the-codedom), note how it specifically mentions the CodeNamespace class to generate the namespace and CodeTypeDeclaration in to generate a class. Google will provide lots of additional tutorials when you query for "system.codedom tutorial". – Hans Passant May 07 '19 at 20:26
  • 1
    Tutorials only provide tutorials for only adding Namespaces, Classes and other stuff to CodeCompileUnit. I need something like CodeCompileUnit, but being able to add any type of statement or declaration there, just like IronPython, IronRuby, F# or IronJS. – Mr.DeleteMyMessages May 08 '19 at 19:30

1 Answers1

1

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.

Georg
  • 5,626
  • 1
  • 23
  • 44
  • But is there any supported version of DLR-based or DLR-like-based stuff? – Mr.DeleteMyMessages May 12 '19 at 15:37
  • 1
    @Mr.DeleteMyMessages Well, the most prominent feature of dynamic languages is that you can access members you statically don't know they exist. For this, you do have support, because at code genartion time, you never really know whether members of generated member accesses exist. – Georg May 13 '19 at 06:23