How to to get all methods in a class and store in a file Using DXCore?

307 views Asked by At

I am trying to write a simple console application as a learner with Dxcore open Api's. I have parsed a C# file using the following line of code.

   LanguageElement   parsedFile = parser.ParseFile(fileLocation)  

and i want to try few basic things on this file. So i was trying to get all methods in this file and its parameters and place them in a list.

I saw a property which would do this but could not use it.

  DevExpress.CodeRush.StructuralParser.TypeDeclaration.AllMethods

Also few links on Dxcore plugin development documentations would be helpful.

Thanks in Advance.

2

There are 2 answers

0
Alex Skorkin On BEST ANSWER

You can cast your "parserFile" reference to the SourceFile type instance and then use the code like this:

  SourceFile parsedFile = parser.ParseFile(fileLocation) as SourceFile;
  if (parsedFile != null)
    foreach (TypeDeclaration type in parsedFile.AllTypes)
      foreach (Method method in type.AllMethods)
        foreach (Param param in method.Parameters)
        {
          // Do something...
        }

This link may probably be a bit of help: How to enumerate solution and source code items using DXCore

1
Rory Becker On

I believe you're looking for the following code.

SourceFile parsedFile = CodeRush.Language.Parse(fileName);
foreach (TypeDeclaration type in parsedFile.AllTypes)
{
    foreach (Method method in type.AllMethods)
    {
        // do stuff
    }
}

Note the change from LanguageElement to SourceFile.