How to convert C# source to CodeDom graph?

A

Alexey Lavnikov

I'm playing with CodeDOM and CSharpCodeProvider. I've found the following
possibilities:

CodeDOM -> Assembly
C# Source code -> Assembly

I miss the C# Source code -> CodeDOM only. Is it there? Or it's hidden under
the hood?
 
V

Vladimir Matveev

As I know in VS2003 method in CSharpCodeDomProvider.CreateParser is not
overridden thus it is always call base implementation that returns
null.
In VS2005 CSharpCodeDomProvider.CreateParser is marked as obsolette but
CSharpCodeDomProvider.Parse method is not implemented
 
A

Alexey Lavnikov

That means, I have to write C# parser myself?
Are there free available implementations already?
 
G

Guest

Alexey Lavnikov said:
That means, I have to write C# parser myself?
Unless you manage to find an existing implementatation, yes. The problem
with a "CSharpToCodeDom" parser is the vastly different expression space of
C# and CodeDom. Some C# expressions are nearly impossible to recreate in
CodeDom. An example:

[C#]
obj as IDisposable
[/C#]

A possible equivalent would be
[C#]
obj is IDisposable ? (IDisposable)obj : (IDisposable)null;
[/C#]

Of course, the expression (boolean ? expr1 : expr2) also has no equivalent.

You'd have to introduce a method.

[CodeDom]
internal T As<T>(object value)
{
if (value != null &&
typeof(T).IsAssignableFrom(value.GetType())
return (T)value;
else
return null;
}

internal T Or<T>(bool condition,
T trueExpression, T falseExpression)
{
if (condition)
return trueExpression;
else
return falseExpression;
}
....
// equivalent to: obj as IDisposable
As<IDisposable>(obj)

// equivalent to: boolean ? expr1 : expr2
Or(boolean, expr1, expr2)
[/CodeDom]

I've no idea how to replicate the checked statement. Other expressions are
just cumbersome to replicate, e.g. is, foreach, using, lock.

Mark

PS: I've found one implementation of ICodeParser:
Microsoft.VisualStudio.Designer.CodeDom.VsCodeDomParser in the assembly
Microsoft.VisualStudio. My guess is that the Form Designer uses that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top