Questions tagged [codedom]

CodeDOM is a framework which can be used to create an (abstract) expression tree representing real code structures (for example, classes, statements, etc.) in a language-independent way. This means if you construct an expression tree, you can use (or write) code generators to output the same logical structure in multiple different target languages. Language generators exist for VB.NET, C#, and JScript, but you can also create your own.

What is CodeDOM?

CodeDOM is a .NET library from Microsoft which:

  • Gives you a way to represent source code as expression objects

  • Provides some pre-built generators to translate the expressions into raw source code

  • Makes it easy to compile the generated source to executable code

Here is an excerpt from MSDN describing CodeDOM:

The CodeDOM provides types that represent many common types of source code elements. You can design a program that builds a source code model using CodeDOM elements to assemble an object graph. This object graph can be rendered as source code using a CodeDOM code generator for a supported programming language. The CodeDOM can also be used to compile source code into a binary assembly.

Some common uses for the CodeDOM include:

  • Templated code generation: generating code for ASP.NET, XML Web services client proxies, code wizards, designers, or other code-emitting mechanisms.

  • Dynamic compilation: supporting code compilation in single or multiple languages.

If you look into CodeDOM you will probably encounter a lot about abstract syntax trees (ASTs). This is the technical term for an expression tree in this context (originating from the way code generators, parsers, and compilers are written). If you plan to get into CodeDOM it helps to understand the basic concept; Wikipedia has a reasonable article on the subject.

What type of question should use this tag?

Questions should use this tag if they relate to the use, comprehension, or interaction with the CodeDOM library. It could also be used on questions relating to generation of code using a document object model (DOM) strategy similar to that provided by the Microsoft CodeDOM library.

Further Reading

593 questions
6
votes
2 answers

C# String representation of method

Is there a way in .NET 3.0 (or earlier) to get a string representation of a method? I know that I can get an IL byte array from a MethodBody object, but I'm interested in getting a string that essentially represents the method body as it appears to…
Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
5
votes
1 answer

Assembly cannot find referenced assembly when compiled to memory with CodeDomProvider

i am trying to compile some code to memory at runtime using a CodeDomProvider. The code I am compiling have a reference to an external assembly which I include in the parameters used when compiling the code. When I compile to memory and try to use…
MartinF
  • 5,929
  • 5
  • 40
  • 29
5
votes
1 answer

Hosting Windows Forms Designer - Serialize designer at runtime and generate C# code

I am creating a designer surface and loading the controls to a runtime. I am having issues when deserializing/loading the controls to the runtime. All methods I have tried seem to have some type of issue. Issued faced for example: Controls are…
TommyBoii
  • 77
  • 1
  • 11
5
votes
4 answers

Parser/Lexer/Programming Language Question

Okay, so I'm not good with all the terms and stuff, BUT here's what I have created thus far: 1: An app that reads character by character the contents of a source code file. And; 2: An app that determines whether character/string 'x' is an…
anon271334
5
votes
1 answer

Can CodeDom create optional arguments when generating a c# method?

Can CodeDom create optional arguments when generating a c# method and provide a default value? For example: public void ExampleMethod(int required , string optionalstr = "default string" , int…
NotDan
  • 31,709
  • 36
  • 116
  • 156
5
votes
2 answers

How to add a partial method without an implementation using CodeDom

internal List createEventHooks() { string[] eventNames = new string[] { "OnUpdate", "OnInsert", "OnDelete", "OnSelect", "OnSelectAll" }; List eventHooks = new…
Chris McCall
  • 10,317
  • 8
  • 49
  • 80
5
votes
1 answer

Which .NET Programming Languages Have a CodeDom Provider?

Aside from C#, VB.NET, C++ (Managed and C++/CLI), and F#, which .NET programming languages have their own CodeDom provider?
plaureano
  • 3,139
  • 6
  • 30
  • 29
5
votes
1 answer

"CompileAssemblyFromSource" in f# powerPack codeDom

I am trying to get going a basic program to dynamically compile and run f# code. I am trying to run the following code: open System open System.CodeDom.Compiler open Microsoft.FSharp.Compiler.CodeDom // Our (very simple) code string consisting…
5
votes
6 answers

Dynamic code generation

I am currently developing an application where you can create "programs" with it without writing source code, just click&play if you like. Now the question is how do I generate an executable program from my data model. There are many possibilities…
codymanix
  • 28,510
  • 21
  • 92
  • 151
5
votes
1 answer

How to express null coalescing operator using CodeDOM?

Let's say, I have following simplified type: public class Model { public decimal? Result { get; set; } } How to express null coalescing operator using CodeDOM to generate C# code, is it possible at all? Now I'm using following workaround: new…
Akim
  • 8,469
  • 2
  • 31
  • 49
5
votes
1 answer

Switch statement in codeDom (jump table style)

I know switch statements are not available in CodeDom and how compilers deal with switch statement. So for performance reasons when many cases are present, I don't want to use If-else Why the switch statement and not if-else? Is is possible to…
Eric Biron
  • 77
  • 7
5
votes
1 answer

Adding and retrieving embedded resources codedom

Ok, I feel like the answer to my question is online, but I cannot find it. All I'm trying to do is add a text resource file to the program I'm compiling with CodeDom and then access that text file in the compiled program. To add the embedded…
user1869878
  • 83
  • 1
  • 6
5
votes
1 answer

How to initialize object with CodeDOM?

I need to initialize my object like below: var obj = new EduBranch { Id = model.Id, WorklevelId = model.WorklevelId, EdulevelId = model.EdulevelId, Title = model.Title, StartEduYearId = model.StartEduYearId, EndEduYearId =…
ePezhman
  • 4,010
  • 7
  • 44
  • 80
5
votes
2 answers

Use DLR to run code generated with CompileAssemblyFromSource?

Following up on this excellent answer, I'm wondering if the DLR using the dynamic keyword can allow a less verbose way of writing code for the generated assembly. For example, can the aforementioned answer's code: using…
user610650
4
votes
1 answer

CodeDom add reference to existing file

In visual studio, I can click on "Reference" > "Add reference" and browse to an existing .dll file from my computer. I can then use the referenced dll as follows: dllNameSpace.dllClassName myReference = new…
user725913