正则表达式解析 C# 源代码 [英] Regex to parse C# source code
问题描述
我正在为以下 C# 源代码创建简单的文档映射.
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()
那么,问题是,是否有处理 C# 源代码的正则表达式或库?
So, question is, is there a regex or library which handles C# source code?
推荐答案
你绝对不能使用正则表达式来解析 C# 源代码,因为 C# 没有正则语法.
You absolutely cannot use a regex to parse C# source as C# does not have a regular grammar.
你可以使用 Roslyn:http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx
You could use Roslyn though: http://blogs.msdn.com/b/csharpfaq/archive/2011/10/19/introducing-the-microsoft-roslyn-ctp.aspx
或者,使用当前的 C# 编译器(或 System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource()
方法)将代码编译为新程序集,加载该程序集并使用反射来分析它.
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.
这篇关于正则表达式解析 C# 源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!