1

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?

Jim
  • 2,760
  • 8
  • 42
  • 66

2 Answers2

7

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.

David Arno
  • 42,717
  • 16
  • 86
  • 131
  • So, even for this simple situation i need to use another library? I will check on Roslyn. – Jim Oct 28 '13 at 15:43
  • 1
    @Jim, parsing the C# language is one of the more complicated tasks you could ask a computer to perform. – Kirk Woll Oct 28 '13 at 22:39
0

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.

Nzall
  • 3,439
  • 5
  • 29
  • 59
  • Thank you ... but I need to implement it in my application and not VS. – Jim Oct 28 '13 at 15:39
  • Why does your app need such an insight into its own code? – Nzall Oct 28 '13 at 15:43
  • As per my description I need a document map of a c# code... – Jim Oct 28 '13 at 15:47
  • 3
    @nzall, unless the OP is asking how to perform a felony, it is thoroughly beside the point to inquire his rationale on why he wants to programmatically perform whatever task he wants to perform. – Kirk Woll Oct 28 '13 at 22:41