如何解析Vstudio C#Project文件并获取所有类名并获取所有方法名和属性 [英] How to parse Vstudio C# Project file and get all class names and get all methodnames and properties

查看:309
本文介绍了如何解析Vstudio C#Project文件并获取所有类名并获取所有方法名和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析C#项目文件并获取所有类名和方法名,以便在C#应用程序中使用类名和方法,如Vstudio的intellisense。

解决方案

我不知道Intellisense如何与Visual Studio一起工作,但我详细了解项目是如何工作的并且可以肯定地说:解析项目不会提供任何必要的帮助 。该项目只包含项目的结构:基本上,源文件,依赖项和源文件的处理方法(事实上,但实际上我列出了所有重要的内容)。通过Reflection,访问所有类型及其所有成员,所有元数据都非常简单。只有一个问题:Intellisense甚至显示了无法编译的代码的方法(参数等)!



如果不是那个特性,基于反射的实现会放轻松。该项目可以在内存中预先构建(重要的是在单独的 AppDomain 中执行,否则会导致巨大的内存泄漏,因为否则编译的程序集不能删除)和获得的元数据。对于无法构建的项目......从理论上讲,这并非不可能,但肯定没有 easy 方法来实现这一目标。



您可以了解Intellisense如何在开源SharpDevelop中实现: http://www.icsharpcode.net/OpenSource/SD/ Default.aspx [ ^ ],但这不一样。



处理项目和构建它们的所有方面都包含在名称空间 Microsoft.Build http://msdn.microsoft.com/en-us/library/gg145008.aspx [ ^ ]。这是一个非常大的主题,包括几个Microsoft程序集。要学习开发Visual Studio Extensions,可以从这里开始: http://msdn.microsoft.com/en-us /library/dd885119.aspx [ ^ ]。



对不起,我无法提供完整的答案。我只是认为这些信息和考虑比没有好。至少它可以帮助你避免在一些错误的线条上浪费宝贵的时间。



-SA


我正在考虑替代答案,以避免编译所有代码,这可能是一个关键 - 请参阅我的第一个答案。



这是什么我来到这里。



您可以使用 System.CodeDom 来获取 CodeDOM程序图,由顶级节点 System.CodeDom.CodeCompileUnit 表示。我看起来像 System.CodeDom.Compiler.CodeDomProvider 可以使用它的 Parse 方法来做到这一点。例如,对于C#,它看起来像这样:



 System.IO.StreamReader reader =  new  System.IO.StreamReader(  MyCode.cs); 
System.CodeDom.Compiler.CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.CodeCompileUnit dom = provider.Parse(reader);





不幸的是,这不是那么容易:a调用 Parse 抛出异常此CodeDomProvider不支持此方法。未实施意味着没有实施;使用此提供程序无法绕过它。我需要一些替代提供者或解析器。



我所知道的唯一实用工具是开源 ICSharpCode.NRefactory 是C#和VB.NET的解析器库: http://wiki.sharpdevelop.net/NRefactory.ashx [< a href =http://wiki.sharpdevelop.net/NRefactory.ashxtarget =_ blanktitle =New Window> ^ ]。



使用此实用程序的源代码应该提供本课程所需的解析数据。

我从未尝试过。



< dd> -SA


我看到它的唯一方法是,你需要使用反射来构建你的项目。

祝你好运!

How can I parse a C# project file and get all class names and method names so that I can use the class names and method like intellisense of Vstudio in my C# application .

解决方案

I don't know how Intellisense works with Visual Studio, but I know in fine detail how a project works and can say for sure: parsing of the project will not provide any essential help. The project only contains a structure of the projects: basically, source files, dependencies and methods of processing of the source files (more than that, in fact but I listed all that matters). Accessing of all the types and all their members, all the meta-data is pretty easy with Reflection. There is only one problem: Intellisense shows even the methods (parameters, etc) of the code which fails to compile!

If not that feature, Reflection-based implementation would be easy. The project could be pre-build in memory (it is important to do it in a separate AppDomain, otherwise it would cause tremendous memory leak, because otherwise a compiled assembly could not be removed) and the meta-data obtained. As to the project that fails to build… Theoretically, this is not impossible, but certainly there is no easy way to accomplish this.

You could learn how Intellisense is implemented in Open Source SharpDevelop: http://www.icsharpcode.net/OpenSource/SD/Default.aspx[^], but this is not the same.

All the aspect of dealing with projects and building them are covered in name space Microsoft.Build: http://msdn.microsoft.com/en-us/library/gg145008.aspx[^]. This is quite a big topic, including several Microsoft assemblies. To learn developing Visual Studio Extensions one can start here: http://msdn.microsoft.com/en-us/library/dd885119.aspx[^].

Sorry I cannot provide complete answer. I just think that this information and considerations are better then nothing. At least it may help you to avoid wasting your valuable time along some of the wrong lines.

—SA


I was thinking about alternative Answer to avoid compilation of all of the code, which can be a key — see my first answer.

Here is what I got here.

You could use System.CodeDom to obtain a CodeDOM program graph, represented by the top-level node System.CodeDom.CodeCompileUnit. I looks like System.CodeDom.Compiler.CodeDomProvider can do it using its Parse method. For C#, for example, it would look like that:

System.IO.StreamReader reader = new System.IO.StreamReader("MyCode.cs");
System.CodeDom.Compiler.CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.CodeCompileUnit dom = provider.Parse(reader);



Unfortunately, this is not so easy: a call to Parse throws exception "This CodeDomProvider does not support this method.". Not implemented means not implemented; there is no a way around it using this provider. I needs some alternative provider or parser.

The only utility I know is the Open Source ICSharpCode.NRefactory which is a parser library for C# and VB.NET: http://wiki.sharpdevelop.net/NRefactory.ashx[^].

Using the source code of this utility should give the parsed data required in this Question.
I never tried though.

—SA


The only way I see it, you need to build your project using reflection.
Best of luck!


这篇关于如何解析Vstudio C#Project文件并获取所有类名并获取所有方法名和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆