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
8
votes
2 answers

How to create a generic class using CodeDOM whose generic parameter is a type that I created?

I have a class that I created using CodeDOM: CodeTypeDeclaration some_class = new CodeTypeDeclaration("SomeClass"); // ... properties and methods creation elided I want to create a List of the "SomeClass" type. However, I can't seem to do so: var…
Bruno Brant
  • 8,226
  • 7
  • 45
  • 90
8
votes
5 answers

Is there some way to automatically indent VB.NET code in a text file

I would like to correctly indent some VB.NET code contained within a text file. Is there some way to do this? e.g. Start with this: Public Shared Function CanReachPage(page As String) As Boolean Try Using client = New WebClient() Using stream =…
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
7
votes
5 answers

Work-around for C# CodeDom causing stack-overflow (CS1647) in csc.exe?

I've got a situation where I need to generate a class with a large string const. Code outside of my control causes my generated CodeDom tree to be emitted to C# source and then later compiled as part of a larger Assembly. Unfortunately, I've run…
mckamey
  • 17,359
  • 16
  • 83
  • 116
7
votes
2 answers

Generate Extension Methods using System.CodeDom

Has anyone ever tried to generate extension methods using System.CodeDom under .NET 4.0? There doesn't seem to be any way to specify a CodeMemberMethod or CodeParameterDeclarationExpression as being an extension method/parameter. If this isn't…
LorenVS
  • 12,597
  • 10
  • 47
  • 54
7
votes
2 answers

ASP.NET CodeDom provider could not be located error

I'm facing an ASP.NET error while deploying on a server: The CodeDom provider type "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral,…
Adam Sancho
  • 177
  • 2
  • 2
  • 13
7
votes
4 answers

How can I use CodeDOM to create and load an assembly in an AppDomain?

I am working on a project that will use CodeDOM to create a class that evaluates a user-defined expression, creates an assembly for the class, and loads the assembly. Since there can be a fair number of user-defined expressions, I would like to…
Welton v3.62
  • 2,210
  • 7
  • 29
  • 46
7
votes
5 answers

Java Code Generation (Metaprogramming, Reflection, wtv)

Does anyone knows a tool for Java (something like codedom for C#) that provides a way to generate Java code to a .java file? EDIT: I'm building a platform the main objective of which is to automate an operation. Giving some input, I want to generate…
7
votes
2 answers

Can we construct an assembly from both CodeExpressions and literal source code at the same time?

I have a situation where one part of my code is generated through CodeExpressions and the other by the user himself (as in: the user simply writes his code as usual, which I would then take and add to my assembly when he compiles). Is it possible to…
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
7
votes
6 answers

C# 4.0, detect if a method is missing

I have a situation where i want to add LinePragmas to CodeDom objects. But some code dom objects have the LinePragma property and some don't. So I'm wondering if it's possible to use the dynamic keyword to detect if the property exists on the…
justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
6
votes
1 answer

Can I run a C# assembly (dll) as 32bit from a 64bit application?

I'm actually doing this with helper executables that are 32bit. But can I do it with DLLs that run on 32bit CodeDOM?
blez
  • 4,939
  • 5
  • 50
  • 82
6
votes
3 answers

How to build CodeCompileUnit from source code?

How to build CodeCompileUnit from source code? What is the best way to parse a C# source code(s)? Is CodeCompileUnit a correct selection? and howto? Thanks
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
6
votes
1 answer

CodeDom - Call a generic method

does anyone know a way to call a generic method of a base class with CodeDom? I have no problem calling a standard method, but I can't find a solution to call the generic. The code I use to call the standard base class method…
Fionn
  • 10,975
  • 11
  • 54
  • 84
6
votes
1 answer

Compile error when using AssemblyCopyrightAttribute or AssemblyCompanyAttribute via CodeDomProvider

I figure something silly is going on as the remaining assembly level attributes can be included just fine but whenever AssemblyCopywriteAttribute or AssemblyCompanyAttribute is declared it results in CS0116 and CS1730 errors. Given that the code…
Tedford
  • 2,842
  • 2
  • 35
  • 45
6
votes
2 answers

Interpreting and/or receiving dotNet code at run-time

Html can contain little bits of Javascript embedded in it (e.g. defined in onclick event handlers). If I were writing an Html browser using a dotNet language like C#, what technologies or APIs could I use to run such Javascript fragments, given…
ChrisW
  • 54,973
  • 13
  • 116
  • 224
6
votes
2 answers

Programmatically Format Generated CodeDom Code

How can I make sure the CS generated from code like the following is formatted nicely, i.e as if we pressed CTRL+K+D? It is C# We are doing something along the lines of: CodeMemberMethod membMethod = new CodeMemberMethod(); membMethod.Attributes =…
Phill Duffy
  • 2,805
  • 3
  • 33
  • 45
1 2
3
39 40