This question already has an answer here:
- Parser for C# 15 answers
i am in the process of creation of simple Document Map for the following C# source.
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string DoSomething(string x)
{
return "Hello " + x;
}
}
The results should be presented in treeview or listview, in following manner:
Person
-- Name
-- Age
-- DoSomething()
So, question is, is there a regex or library which handles C# source code?
Visual Studio already has something of the sort. you can use the arrow next to a CS file to click through and get a list of all methods and variables in that file. it even shows a padlock if they're private.
You absolutely cannot use a regex to parse C# source as C# does not have a regular grammar.
You could use Roslyn though: http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx
Alternatively, use the current C# compiler (or the
System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource()
method) to compile the code to a new assembly, load that assembly and use reflection to analyse it.