How to programmatically parse and modify C# code
Asked Answered
R

6

12

What I want to do is to read C# code, parse it, insert some method calls and compile it finally.

Is it possible to convert C# source code (a list of strings) to CodeDOM objects?

Retardment answered 3/10, 2009 at 16:15 Comment(0)
R
6

It is not directly possible to do this with the core .NET Framework. You need to use third party or add-on tools, for example:

Rodmun answered 3/10, 2009 at 17:19 Comment(2)
But why? Does the C# compiler don't use CodeDOM?Retardment
No, it does not. CodeDom is for managed application developers; csc.exe is unmanaged code which has no .NET dependencies.Rodmun
K
11

This is a really old question, but is worth noting that the accepted answer no longer applies. Microsoft's recent Roslyn project is explicitly aimed at exposing all the knowledge the compiler gains about your codebase in the process of statically analyzing it, and exposing all this information through managed APIs for you to leverage. It is available for both VB and C#.

Since you want to consume static analysis information, you'll need the Microsoft.CodeAnalysis NuGet package (the stuff you need for C# is under the Microsoft.CodeAnalysis.CSharp namespace) and some time at the samples and walkthroughs page in the docs.

Kali answered 4/6, 2015 at 23:9 Comment(0)
R
6

It is not directly possible to do this with the core .NET Framework. You need to use third party or add-on tools, for example:

Rodmun answered 3/10, 2009 at 17:19 Comment(2)
But why? Does the C# compiler don't use CodeDOM?Retardment
No, it does not. CodeDom is for managed application developers; csc.exe is unmanaged code which has no .NET dependencies.Rodmun
I
2

Try Linq over C#. It's wonderful.

Indochina answered 3/10, 2009 at 16:17 Comment(0)
T
0

Try Mono.Cecil

Towrey answered 3/10, 2009 at 16:33 Comment(1)
Correct, it deals with IL only. You could use NRefactory (which is a parser for C# and VB.NET, also from the #Develop project).Ravo
B
0

If you want the ability to parse, and carry out arbitrary analyses and transformations on C# source code (or a variety of other languages), check out our The DMS Software Reengineering Toolkit.

DMS has a full C# front end, builds complete abstract syntax trees for parsed code (but not a CodeDom), provides a full procedural API for walking/checking/changing the ASTs. After revising the tree, DMS can regenerate source code corresponding to the modified tree, either in fidelity mode where it tries to preserve the original spacing, or prettyprint mode where it applies a prettyprint style that you can control completely. Comments are retained in the regenerated source properly.

In addition, DMS provides source-level pattern matching and transformation (e.g, you can write "x=x+1 ==> x++" instead of coding all the walk-around-tree-to-verify, hack the tree to change.) See writeup on program transformations to understand what why this is a lot less work..

Baucis answered 3/10, 2009 at 17:21 Comment(0)
C
-2

The CSharpCodeProvider might be what you're looking for.

Cracow answered 3/10, 2009 at 16:32 Comment(1)
NO. I don't think CSharpCodeProvider can do what I described above.Retardment

© 2022 - 2024 — McMap. All rights reserved.