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
4
votes
1 answer

Complex attribute declaration for methods in CodeDom

I'm trying to generate some methods using CodeDom, I have a problem generating custom attributes for these methods. I can manage simple empty attributes, such as [DataMember()] or attributes with string value arguments, [DataContract(Namespace =…
Shaggydog
  • 3,456
  • 7
  • 33
  • 50
4
votes
3 answers

Generate private setters using CodeDOM

I am currently developing an API that is compiled dynamically to an assembly based up some predefined rules stored in XML documents. I am having a hard time trying to get CodeDOM to generate properties with a public getter and private setter…
Intrepid
  • 2,781
  • 2
  • 29
  • 54
4
votes
3 answers

What controls version number inside codedom generated file?

What controls the version number inside of a codedom generated file? Some of our developers get: //------------------------------------------------------------------------------ // // This code was generated by a tool. // …
RMK
  • 877
  • 1
  • 9
  • 16
4
votes
1 answer

Does a CodeDom Visual Basic .NET parser exist?

Does somebody know if someone has created a Visual basic parser to CodeDom (ie, it takes VB.NET as input and create a CodeCompileUnit graph) ? SOLUTION I've retrieved the source code of SharpDevelop, and used their parser, it works just fine !
Nicolas Dorier
  • 7,383
  • 11
  • 58
  • 71
3
votes
1 answer

How to make user-entered C# code safe?

I am building a conditional formatting feature, where the user can enter an expression like someFieldValue == "someValue" And we apply formatting based on the result of that expression. The quickest way to accomplish this (with a full-featured…
tenfour
  • 36,141
  • 15
  • 83
  • 142
3
votes
3 answers

CSharpCodeProvider Conform to Interface

I have a CSharpCodeProvider which takes in the code for a class. In the project where the code is being compiled, I have an interface. I would like the code I'm compiling to conform to this interface. Here's the simplest example I could think of…
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
3
votes
2 answers

How to check for compile errors before compiling with CodeDom

I'm using the CodeDom to allow custom scripts (C#) to be run in an application I'm creating. While writing the script I would like to be able to check for compile errors. The code gets added to and compiled into memory at a much later time and run…
thilinab
  • 117
  • 2
  • 8
3
votes
1 answer

C# Using CodeDom to add variables as part of a class

I am attempting to create the following code through the use of CodeDom: public partial class mainClass { public byte[] bytes = null; } I have no problem creating the class, and I found ways to declare variables through the use of CodeDom using…
user725913
3
votes
2 answers

Can I use Reflection.Emit for generating code and save generated codes in .cs files or I could use CodeDom?

I want to write a code generator and save these codes with mvp pattern, can I use Reflection.Emit as a solution or CodeDom is better? EDIT-------------- I have to do 2 work, firstly I want to compile this code at runtime and run it and secondly…
masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
3
votes
2 answers

CodeDom compiler: Type or namespace 'Foo' does not exist in the namespace 'System'

I'm writing a tool that generates C# code, and am writing unit tests to go with it. One of the tests I want to do is to run the tool and compile the results, so I can examine the assembly with reflection. However, I am having trouble doing this. My…
Mike Caron
  • 14,351
  • 4
  • 49
  • 77
3
votes
4 answers

Creating extension method using CodeDOM

I'm trying to create an extension method using CodeDOM. There doesn't seem to be any support for them and using ExtensionAttribute (which C# uses internally to mark extension methods) is not allowed. It's possible to use a trick to specify the this…
svick
  • 236,525
  • 50
  • 385
  • 514
3
votes
1 answer

Setting file version for a codeDOM file

I'm looking for ANY means of setting the file version for an exe file generated using codeDOM. Mine always comes out as 0.0.0.0. Programatically would obviously be preferred, but at this point anything would be better than nothing.
Mcham
3
votes
2 answers

Extra parentheses in CodeDom-generated code

I'm using CodeDom to generate code to be compiled later, and I've noticed that certain constructs create extra sets of parentheses. While I know they don't affect anything, they do look strange. A sample of code that does it is this: new…
Mike Caron
  • 14,351
  • 4
  • 49
  • 77
3
votes
1 answer

setting private properties of classes

I have some very old code which uses reflection to set properties of objects, e.g something like this: var properties = obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (var property in…
M4N
  • 94,805
  • 45
  • 217
  • 260
3
votes
0 answers

How to make an async call and function definition using CodeDom?

I've recently started working with CodeDom to generate classes and interfaces, but never to define functions or create and start a Task. I need to replicate this code using CodeDom: public async Task
philr
  • 1,860
  • 1
  • 21
  • 31