Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have the path of the cs file.

What I have tried:

I have used basic string operations to get the functions after scanning the whole file. I'm looking for a good method.
Posted
Updated 13-Apr-17 19:11pm
v2
Comments
Tomas Takac 10-Apr-17 3:36am    
Did you try regular expressions[^]?

There is no "good method" if you can't use reflection - you have to manually parse the whole file and establish exactly what the content is. That means identifying namespaces (because a file can legitimately contine multiple namespaces), classes (because a class of the same name can exist in multiple namespaces, and classes can be embedded inside other classes), comments (because C# includes /* and // comment forms and it's valid to comment out whole classes with a /* ... */ block), #if blocks, #else blocks, etc.

Seriously? Use reflection.
 
Share this answer
 
Comments
AkashSuman 10-Apr-17 4:06am    
I can use reflection but the problem is I only have the path of the file. How am I going to use the path in reflection ?
OriginalGriff 10-Apr-17 4:18am    
Use CSC.EXE to compile it and then use reflection to load the assembly.
If you don't, you basically have to write your own first / second stage C# compiler to parse the file adequately.
If I were doing this, I'd use the Visual Studio SDK that allows you to build extensions and tools that work inside Visual Studio. This SDK provides the ability to parse classes directly inside Visual Studio itself.
 
Share this answer
 
I'd be inclined to use Roslyn for this:
GitHub - dotnet/roslyn: The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs.[^]
Getting Started with Roslyn - Part 1[^]

It's the same compiler that VS2015/2017 use, so it should handle anything you can throw at it.
 
Share this answer
 
v2
I have used Roslyn and it gives all the method names.

public static List<string> GetAllMethodNames(string strFileName)
       {
           var tree = SyntaxTree.ParseFile(strFileName);
           var members = tree.GetRoot().DescendantNodes().OfType<MemberDeclarationSyntax>();
           List<string> methodNames = new List<string>();
           foreach (var member in members)
           {
               var method = member as MethodDeclarationSyntax;
               if (method != null)
                   methodNames.Add(method.Identifier.ToString());
           }
           return methodNames;

       }
 
Share this answer
 

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