SubMain - CodeIt.Right The First Time!

/Community

Support Community for SubMain Products
 Home Products Services Download Purchase Support
in Search
 
Home Forums Blogs Tutorials/CIR Tutorials/GD Downloads
Welcome to SubMain Community Sign in | Join | Help

how to get classinfo array from projectfilesinfo

Last post 11-05-2014, 3:29 AM by Misha Zhutov (SubMain). 1 replies.
Sort Posts: Previous Next
  •  11-04-2014, 9:43 PM 4005

    how to get classinfo array from projectfilesinfo

    I want to make a custom rule "Common methods should not be used". My ruletarget is Project. My logic is to get all the methods names in a project and then check for duplication. I know how to get method name using methodinfo.DisplayName property. My check method code is 

                IProjectInfo pinfo = element as IProjectInfo;

     

                IProjectFileInfo[] pfilesinfo = pinfo.GetProjectFiles();

     

                for (int i = 0;i < pfilesinfo.Length; i ++)

                {

                         //how to get classinfo array from projectfilesinfo ?? IProjectFileInfo doesn't have any method to get iclassinfo

                } 

     

     

     

                

     

     

  •  11-05-2014, 3:29 AM 4008 in reply to 4005

    Re: how to get classinfo array from projectfilesinfo

    Hello,

    For that rule we recommend to use Method as a target.
    In addition, your custom rule project should reference should reference SubMain.CodeItRight.Core.dll and SubMain.CodeItRight.CodeObjectModel.dll assemblies (from installation folder)

    Use the following usings and method for getting all project methods:

    using SubMain.CodeObjectModel.Reflection;
    using SubMain.Core.Services;

      private List<IMethod> GetProjectMethods(IProjectInfo projectInfo)
      {
       var methodList = new List<IMethod>();

       IParserService parserService = (IParserService)ServiceManager.Services.GetService(typeof(IParserService));
       var compilationUnits = parserService.GetAllCompilationUnits();
       if (compilationUnits != null)
       {
        for (int i = 0; i < compilationUnits.Count; i++)
        {
         ICompilationUnit unit = compilationUnitsIdea;
         if (unit != null &&
          string.Compare(unit.Project.FileName, projectInfo.FileName, StringComparison.CurrentCultureIgnoreCase) == 0)
         {
          var types = unit.TypesTable;
          if (types != null)
          {
           for (int j = 0; j < types.Count; j++)
           {
            var type = unit.TypesTable[j] as IClassInterface;
            if (type != null && type.Methods != null)
            {
             methodList.AddRange(type.Methods.GetArray());
            }
           }
          }
         }
        }
       }

       return methodList;
      }


    As you can see CodeIt.Right has two object models for methods (IMethodInfo and IMethod). IMethod is for interncal usage but has complete set of properties.

    If you need to transfer from IMethodInfo to IMethod you need to run:

    IMethod method = SubMain.CodeItRight.Sdk.CodeElementFactory.GetMethod(methodInfo);

     


    Thank you,
    Misha Zhutov
    http://submain.com - .NET Developer Tools | Microsoft VB MVP
View as RSS news feed in XML
 
     
 
Home |  Products |  Services |  Download |  Purchase |  Support |  Community |  About Us |