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

583 questions
114
votes
4 answers

Microsoft Roslyn vs. CodeDom

From a press release yesterday on InfoWorld regarding the new Microsoft Roslyn: The most obvious advantage of this kind of "deconstructed" compiler is that it allows the entire compile-execute process to be invoked from within .Net…
mellamokb
  • 53,762
  • 11
  • 101
  • 131
100
votes
2 answers

Is the VC++ code DOM accessible from VS addons?

Visual Studio IntelliSense for VC++ includes the "complete" EDG C++ parser (also used by Intel and others). Since the C# Code DOM is accessible to addons (correct me if I'm wrong), is the C++ Code DOM also accessible? Can this be used to analyse an…
Robin Rodricks
  • 99,791
  • 133
  • 372
  • 575
52
votes
3 answers

Reflection.Emit vs CodeDOM

What are some pros/cons for using the Reflection.Emit library versus CodeDOM for dynamically generating code at runtime? I am trying to generate some (relatively complicated) dynamic classes in a system based on metadata available at runtime in XML…
LBushkin
  • 121,016
  • 31
  • 208
  • 258
39
votes
5 answers

Using C# 6 features with CodeDomProvider (Roslyn)

CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp" ); CompilerParameters objCompilerParameters = new CompilerParameters(); ... CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile(…
Derek
  • 6,565
  • 3
  • 28
  • 47
36
votes
1 answer

What exactly does / do in web.config in MVC 5?

I am about to migrate a bunch of projects from .NET 4.0 + MVC 3 to .NET 4.5.2 + MVC5. To make this easier, I've created a new blank MVC project to compare DLL references and some other stuff such as web.config. In the latter, the following entries…
MarioDS
  • 11,911
  • 14
  • 57
  • 114
28
votes
1 answer

Preserve serialization-order of members in CodeDOM

I know we can enforce generating the members within the classes in the same order as within the members-collection as stated on MSDN. However I look for some code that also adds a serialization-attribute providing the order of those members. So this…
HimBromBeere
  • 32,045
  • 4
  • 46
  • 84
27
votes
4 answers

How to Read an embedded resource as array of bytes without writing it to disk?

In my application I compile another program from source.cs file using CodeDom.Compiler and I embed some resources ( exe and dll files ) at compile time using : // .... rest of code if (provider.Supports(GeneratorSupport.Resources)) { …
Rafik Bari
  • 4,473
  • 17
  • 61
  • 121
20
votes
6 answers

Execute JavaScript from within a C# assembly

I'd like to execute JavaScript code from within a C# assembly and have the results of the JavaScript code returned to the calling C# code. It's easier to define things that I'm not trying to do: I'm not trying to call a JavaScript function on a web…
ScottKoon
  • 3,403
  • 5
  • 24
  • 29
18
votes
2 answers

Is there a way to have CodeDom put using statements before the namespace

The msdn documentation says add namespaces imports to the CodeNamespace.Imports collection. This puts them inside the namespace (which makes sense, since your adding them to the namespace) namespace Foo { using Bar; //Code } However the rest…
Josh Sterling
  • 828
  • 7
  • 12
15
votes
1 answer

How can I target a specific language version using CodeDOM?

Using the C# code provider and the ICodeCompiler.CompileAssemblyFromSource method, I am attempting to compile a code file in order to produce an executable assembly. The code that I would like to compile makes use of features such as optional…
Caster Troy
  • 2,758
  • 2
  • 20
  • 44
14
votes
2 answers

Can you pass a variable into the C# compiler code?

Here's my current situation - I have an application that compiles C# code taken in as a string, using CodeDom. I have a SecureString that stores a password and I was wondering if there would be any way to pass that SecureString variable into the…
Jake
  • 183
  • 7
14
votes
2 answers

Debugging a generated .NET assembly from within the application that generated it

The question in short: How can I debug the code generated during a debugging session on the generating program? (see code below) I am facing the following issue: I would like to debug into dynamically generated/compiled code from the application…
jdehaan
  • 19,108
  • 6
  • 54
  • 94
12
votes
2 answers

CodeDom generic type constraint

Is there a way to generate a class constraint with CodeDom. Because when I use something like var method = new CodeMemberMethod(); var genericParam = new…
Fionn
  • 9,905
  • 10
  • 52
  • 79
12
votes
1 answer

SerializationStore not finding references

When trying to deserialize using the ComponentSerializationService, errors are populated that references were not found: public ICollection Deserialize(object serializationData) { var serializationStore = serializationData as…
JL.
  • 71,902
  • 119
  • 298
  • 446
11
votes
2 answers

C# CodeDom Automatic Property

I have a property created with CodeDom. How can I set it to being an automatic property instead of adding CodeFieldReferenceExpressions against a private member?
blu
  • 12,033
  • 18
  • 66
  • 102
1
2 3
38 39