Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
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 .
Posted
Comments
Sergey Alexandrovich Kryukov 6-Feb-11 21:00pm    
Interesting question.

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
 
Share this answer
 
v3
Comments
Espen Harlinn 6-Feb-11 5:53am    
Good answer, as usual :)
Sergey Alexandrovich Kryukov 6-Feb-11 21:20pm    
Thank you, Espen.
Well, my answer is incomplete! I immediately though about CodeDOM, but it turned out to be not so fast :<. Anyway, please see my alternative Answer. The utility from NRefactory is maybe the closest to the solution.
--SA
Manfred Rudolf Bihy 7-Feb-11 3:21am    
Good one! 5+
Sergey Alexandrovich Kryukov 7-Feb-11 21:17pm    
Thank you, Manfred.
There is something else to thing about...
--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:

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);


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
 
Share this answer
 
v2
Comments
JF2015 7-Feb-11 0:22am    
Good answer.
Sergey Alexandrovich Kryukov 7-Feb-11 21:16pm    
Thank you.
--SA
Manfred Rudolf Bihy 7-Feb-11 3:20am    
Agreed! 5+
The only way I see it, you need to build your project using reflection.
Best of luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Feb-11 21:17pm    
Maybe the alternative way does exists. Please see my second (alternative) Answer as well as the concerns related to Reflection in my first Answer.
Thank you.
--SA
First grab a package by typing the following in VS' Package Manager Console:

Quote:
Install-Package Microsoft.CodeAnalysis -Pre


Congrats! you have Roslyn[^] capability in your project, which means you can certainly do something like:

C#
string code = @"C# or VB code, read from file, network, device or any thinkable source";
SyntaxTree ast = SyntaxTree.ParseCompilationUnit(code); // Abstract syntax tree provider
CompilationUnitSyntax root = ast.Root as CompilationUnitSyntax;


Now, to get usings, you can do something like: root.Usings.

Or even traverse the decendents using DescendentNodes. You can also find the descendents of interest using Linq:

C#
ExpressionSyntax chunks =   root.DescendentNodes()
                                .OfType<expressionsyntax>()
                                .FirstOrDefault();


To find syntax types to which you can cast, see this blog[^] use your favorite .NET disassembler to explore.

HTH.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900