Playing around with the CodeDOM

Saturday, December 7, 2013

I am reading the book Metaprogramming in .Net which gives a solid introduction about metaprogramming and the various techniques and tools you can use when doing metaprogramming in .Net.

One of the described techniques is the CodeDOM. The CodeDOM is nothing new and has been there since the beginning of .Net. With the CodeDOM you can build object graphs which you can use to express code as data. The newer Expression Trees introduced in version 3.0 of .Net are something similar but work differently and don’t provide the same features. Meaning expression trees won’t replace the CodeDOM.

An object graph can be converted to source code or even be compiled into an assembly. This creates powerful scenario’s like building code generators or do dynamic compilation.

The book describes the CodeDOM structure of the following line of code which consists of one statement and a bunch of expressions.

R = fn(A + B) / C

Assigning the result of the expressions: fn(A + B) / C to expression R is the statement. The book doesn’t provide a code example to build the object graph to express this line of code using the CodeDOM. So that makes a perfect example to try out our self. 

Besides the expressions and statements I used in the example there are lot more you can use to build your object graph.

Filed Under: Programming
Comments are closed