Can you explain very briefly what CodeDOM is and used for with a simple real life example?
Asked Answered
P

2

9

Can you explain very briefly what CodeDOM is and used for with a simple real life example?

A simple example that covers why would I need it as a developer, in what scenarios?

Thanks

Protuberant answered 15/4, 2011 at 7:25 Comment(0)
A
6

CodeDOM stands for Code Document Object Model. Basically, it is your code represented by a hierarchical tree of objects, a model.. Consider a method with some statements in it:

int Foo(int bar)
{
    int i = 0;
    if ( bar == 1 ) i = 1;
    return i;
}

This would result in a DOM as follows:

method foo
    declaration
    if (expression)
        assignment
    return

A model like this that represents your code allows you to perform various manipulations or checks on it.

Avowal answered 15/4, 2011 at 7:29 Comment(8)
Can you describe how this is related to an abstract syntax tree?Mangum
I know what ASTs are, sure, and I really know how to use them. The DOM puzzles me. It looks like a bad program representation; it doesn't appear to be right for anything (C#, VB.net, ...). It doesn't have enough detail to capture expressions as I understand. The MS docs don't say what its purpose really is. You say you can "perform various manipulations" on it. Like what? Can I parse C# into a DOM? Can I build a DOM and produce C#? Can I produce an arbitrary C# program? Why/why not?Mangum
Actually, I am more or less explaining the concept of a CodeDom, not the specific implementation. You are actually mostly correct however. The CodeDom implementation included by microsoft is only half finished. You cannot turn C# into A CodeDom but you can turn CodeDom into code and even an assembly. Even so, not even every language feature is implemented.Avowal
The whole reason we (the compiler community, not just me) use ASTs is so we don't get some kind of half-baked program representation. I've seen lots of attempts to build a "generic program representation". Most of them get further than the MS CodeDom has, and still fail because they can't represent all the precise details of the program. (The argument that expressions are represented ... as strings is a non-starter for me; the reason to have a parser building the AST is to avoid having to parse when processing stuff). So who uses CodeDom? You can't use it for analysis.Mangum
Do you hear me promoting the CodeDom microsoft implemented? or do you hear me explaining what a DOM of code is? I don't know if I would describe codedom as an AST though. There's not much 'syntax' in it left.Avowal
No, I don't hear you promoting it, and wasn't trying to make you defend it or its design. You're just the first guy that sounded like he knew something about it. Curiousity killed the cat. Sorry.Mangum
There's hardly reason to apologize. I do know something about it because like many others I have a moderate interest in compilers, code, tree generation, symbol tables and such. Inevitably you are bound to run into the Codedom already included and think 'Hey! This is great!' (after all, you won't need to build your ast). And then .. well. You realize the truth and are able to spread the word. But you and I talked before on different questions.Avowal
So, aside the verbiage, what is CodeDom used for? I am still puzzled. And the question was about CodeDom not Dom.Garibaldi
S
5

The CodeDOM represents the code in a structured manner. You would want to use it, if you would like to write a program that analyses code like ReSharper or that generates code. For more info, see: http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx

Stylo answered 15/4, 2011 at 7:30 Comment(5)
@Andrew: I don't understand your comment, please explain.Stylo
It's nothing important :) Just giving a +1Hogg
Hehe, thanks. Your comments in this question sure are strange... Especially, the one you just deleted ;-)Stylo
You'd want to use it to analyse code, if they'd ever implemented the parsing side of things. AFAIK, the parsing side has never been completed.Bloodworth
It appears to be not very useful for analyzing stuff. See comments on other answers....Mangum

© 2022 - 2024 — McMap. All rights reserved.