CSharpCodeProvider.Parse"为"替代; [英] An alternative for "CSharpCodeProvider.Parse"
问题描述
我在寻找的 CSharpCodeProvider.Parse
的替代品。该方法应该解析[C#]的源代码和返回 CompileUnit
对象。然而,该方法不以任何的.Net框架中实现
I'm looking for an alternative for CSharpCodeProvider.Parse
. The method was supposed to parse a [C#] code source and return a CompileUnit
object. However, the method isn't implemented in any .Net framework.
我的目的是能够浏览一个C#的CodeDOM无需编译它。我正在写一个应用程序,做了一些代码分析,但我不一定会拥有所有外部引用,这意味着我不能编译。
My purpose is to be able to navigate a C# CodeDOM without having to compile it. I'm writing a application that does some code analysis but I won't necessarily have all external references, which means I can't compile it.
推荐答案
的SharpDevelop(开源IDE常用于单声道)有一个名为NRefactory库,使您解析C#代码,并将其转换成AST:的 http://wiki.sharpdevelop.net/NRefactory.ashx (摘录自该链接如下):
SharpDevelop (the open source IDE commonly used for Mono) has a library called NRefactory that allows you to parse C# code and convert it into an AST: http://wiki.sharpdevelop.net/NRefactory.ashx (Excerpt from that link follows):
using (IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(sourceCode)))
{
parser.Parse();
// this allows retrieving comments, preprocessor directives, etc. (stuff that isn't part of the syntax)
specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
// this retrieves the root node of the result AST
result = parser.CompilationUnit;
if (parser.Errors.Count > 0) {
MessageBox.Show(parser.Errors.ErrorOutput, "Parse errors");
}
}
这篇关于CSharpCodeProvider.Parse"为"替代;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!