Getting all the classes in project and their packages from CodeRush extension

落爺英雄遲暮 提交于 2020-01-13 07:03:07

问题


I've spent some time trying to find a way CodeRush could add using when it finds undeclared element that is in the fact class name with no using added. The solution suggested in this answer to my question (Refactor_resolve) does not work (bugged?).

In a process I found out that writing plug-ins for CodeRush is easy, so I decided to code this functionality myself (and share it). I'd only implement a CodeProvider (like in this tutorial). The only thinks I need to do the job are answers to this questions:

  1. At the start up of my plugin I need to get a list (set, map, whatever) of all classes and their packages. This means all the classes(interfaces...) and their packages in project, and within all referenced libraries. And I also need to receive updated on this (when user adds reference, creates new class). Can I get this from some CodeRush classes or maybe VS interface available from CodeProvider class?

  2. How do I add created CodeProvider to the pop-up that is shown when user hovers over an Issue?


回答1:


BTW, it looks like Rory has fixed some bugs in the "Refactor_Resolver" plug-in and it works now. As for your questions here's a quick reply:

RE #1:

CodeRush has a built-in cache for all types, project references, etc that is built while the solution is parsing. But at the moment it is used internally and not exposed for plug-in devs, sorry. However, here are some useful APIs to get started with:

  // Using the source code cache...

  // gets the active Solution object
  SolutionElement activeSolution = CodeRush.Source.ActiveSolution;
  if (activeSolution == null)
    return;

  // iterate thought all projects in the solution
  foreach (ProjectElement project in activeSolution.AllProjects)
  {
    string assemblyName = project.AssemblyName;

    // iterate inside source code symbols cache...
    Hashtable projectSymbols = activeProject.ProjectSymbols;
    foreach (object item in projectSymbols.Values)
    {
      ITypeElement typeElement = item as ITypeElement;
      if (typeElement == null)
        continue;

      // TODO: ...
    }
  }

To get assembly references cache, use a ScopeManager (located in DevExpress.DXCore.MetaData.dll), e.g.

  IEnumerable<IMetaDataScope> allMetaDataScopes = ScopeManager.All;
  foreach (IMetaDataScope scope in allMetaDataScopes)
  {
    IAssemblyInfo assembly = scope.Assembly;
    if (assembly != null)
    {
      ITypeInfo[] types = assembly.GetTypes();
      for (int i = 0; i < types.Length; i++)
      {
        ITypeInfo typeInfo = types[i];
        if (typeInfo == null)
          continue;

        // TODO: ...
      }
    }
  }

RE #2: To add a CodeProvider to the pop-up set its "CodeIssueMessage" property to the name of the code issue to fix, e.g.

myCodeProvider.CodeIssueMessage = "Undeclared element";

Let me know if you need further assistance.



来源:https://stackoverflow.com/questions/2886369/getting-all-the-classes-in-project-and-their-packages-from-coderush-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!