使用 DXCore 控制台应用程序获取解决方案/项目中的文件列表

发布于 2024-11-11 07:44:46 字数 284 浏览 3 评论 0原文

据我了解,以下代码片段可用于在插件中使用时提取 VS 解决方案信息。

EnvDTE.Solution solution = CodeRush.ApplicationObject.Solution;
EnvDTE.Projects projects = solution.Projects;

问:我想构建一个控制台应用程序并访问这些文件详细信息。我的目标是创建一个控制台应用程序(可以在没有 VS 的情况下运行)来根据我在输入 .sln 文件中发现的设计问题生成报告。我为此使用什么函数?

I understand that the following snippets can be used to extract a VS solution info when used in a plug-in.

EnvDTE.Solution solution = CodeRush.ApplicationObject.Solution;
EnvDTE.Projects projects = solution.Projects;

Q: I would like to build a console application and access these file details. My aim is to create a console application (that can be run without VS) to generate a report based on the design issues I find in the input .sln file. What functions do I use for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-11-18 07:44:46

原始(和更新的)帖子位于 这里

实际上,DXCore 并非设计用于在 Visual Studio 之外使用,但总有解决方法...在本文中,我将向您展示如何在常规 C# 控制台应用程序中使用 DXCore 框架来解析整个解决方案并使用抽象解析树。该解决方案应作为 *.sln 文件的完整路径作为参数传递给程序。如果没有使用参数,则使用测试程序的硬编码路径,因此程序将解析自身并打印有关解决方案的信息,例如使用的所有类型的列表以及每个类内的成员数量。

让我们创建一个新的 C# 控制台应用程序,将其命名为 TestDXCoreConsoleApp 并将其保存在“C:\Project”文件夹中:

创建新控制台App

然后,我们应该将新项目的 Target Framework 版本更改为 Framework 4.0,因此它不是“Target Framework 4.0 Client Profile”,因为某些必需的程序集引用不支持此版本的 Target Framework:

设置目标框架

现在,添加所需的程序集引用。以下是我们需要的列表:

  1. DXCore 程序集:
  • DevExpress.CodeRush.Common
  • DevExpress.CodeRush.Core
  • DevExpress.CodeRush.StructuralParser
  • DevExpress.CodeRush.VSCore
  • DevExpress.DXCore.AssemblyResolver
  • DevExpress.DXCore.Parser

这些程序集可以在 DevExpress IDE 工具中找到安装文件夹。例如,路径可能如下所示:

C:\Program Files\DevExpress 2011.1\IDETools\System\DXCore\BIN

  1. 现在,用于不同程序语言支持的三个附加程序集:
  • DX_CPPLanguage
  • DX_CSharpLanguage
  • DX_VBLanguage

通过这些程序集,我们能够解析 CSharp, Visual Basic 和 C++ 项目。它们可以在这里找到:

C:\Program Files (x86)\DevExpress 2011.1\IDETools\System\DXCore\BIN\SYSTEM

  1. .NET Framework 程序集:
  • Microsoft.Build.BuildEngine.dll
  1. 最后,还有几个 Visual Studio 程序集:
  • EnvDTE
  • VsLangProj

这两个可以在“PublicAssemblies”文件夹中找到:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\

现在,DXCore 支持代码。需要此代码来加载解决方案、其项目并初始化 DXCore 解析器。我添加了两个文件夹:

  1. Helpers 文件夹包含以下类:
  • LanguageHelper.cs - 检测项目的语言(例如 CSharp、Visual Basic 或 C++)。
  • ParserHelper.cs - 初始化 DXCore 解析器和一些重要的 DXCore 服务 - 用于解析源代码的源模型服务和语言服务。
  • SolutionParser.cs - 一个帮助器类,它采用您要解析的解决方案的路径。调用 GetParsedSolution 方法将返回 SolutionElement,它保存整个解决方案的抽象源树。
  1. Loaders 文件夹包含不同 Visual Studio 版本的 Visual Studio 项目和解决方案加载器。它们用于解析 *.XXproj 和 *.sln 文件。有VS2002、VS2003和VS2005的版本。 VS2008 和 VS2010 没有专用的加载器,因为旧 VS 版本的加载器非常适合读取和加载较新的 Visual Studio 项目和解决方案格式文件(例如 2008、2010)。

以下是 TestDXCoreConsoleApp 的最终结构:

DXCore ConsoleApp 结构

具有完整源代码的 TestDXCoreConsoleApp 为 此处(267,457 字节,C#,VS2010),因此您可以查看代码并按您的方式使用它我喜欢。以下是 Program 类的 Main 函数:

static void Main(string[] args)
{
  string SolutionPath;
  if (args != null && args.Length > 0)
    SolutionPath = args[0];
  else
    SolutionPath = @"c:\Projects\TestDXCoreConsoleApp\TestDXCoreConsoleApp.sln";

  try
  {
    ParserHelper.RegisterParserServices();

    Console.Write("Parsing solution... ");

    SolutionParser solutionParser = new SolutionParser(SolutionPath);
    SolutionElement solution = solutionParser.GetParsedSolution();
    if (solution == null)
      return;

    Console.WriteLine("Done.");

    foreach (ProjectElement project in solution.AllProjects)
      foreach (SourceFile file in project.AllFiles)
        foreach (TypeDeclaration type in file.AllTypes)
        {
          Console.Write(type.FullName);
          Console.WriteLine(", members: " + ((ITypeElement)type).Members.Count);
        }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
  }
  finally
  {
    ParserHelper.UnRegisterParserServices();
  }

  Console.ReadLine();
}

如果将源代码放入“C:\Projects”文件夹并在不指定任何参数的情况下运行程序,您应该看到以下结果:

DXCore 控制台应用程序结果

按 Enter 键关闭窗口。请记住,解析过程可能需要一些时间,因此您可能需要等待几秒钟,直到解析整个解决方案。

The original (and updated) post is located here.

Actually, DXCore is not designed to be used outside of Visual Studio, but there are always workarounds... In this article I'm going to show you how to use the DXCore Framework inside the regular C# Console Application to parse an entire solution and work with the abstract parsed tree. The solution should be passed-in as an argument to the program as a full complete path to the *.sln file. If there's no argument used, the hard-coded path to the test program is used, so the program will parse itself and print information about the solution, such as a list of all types used and the number of members inside of each class.

Let's create a new C# Console Application, call it TestDXCoreConsoleApp and save it inside the "C:\Project" folder:

Creating a new Console App

Then, we should change the Target Framework version of the new project to Framework 4.0, so it's not a "Target Framework 4.0 Client Profile", because some required assembly references don't support this version of the Target Framework:

Settings Target Framework

Now, let add required assembly references. Here's the list of what we need:

  1. DXCore assemblies:
  • DevExpress.CodeRush.Common
  • DevExpress.CodeRush.Core
  • DevExpress.CodeRush.StructuralParser
  • DevExpress.CodeRush.VSCore
  • DevExpress.DXCore.AssemblyResolver
  • DevExpress.DXCore.Parser

These assemblies canbe found inside your DevExpress IDE Tools installation folder. For example, the path may look like this:

C:\Program Files\DevExpress 2011.1\IDETools\System\DXCore\BIN

  1. Now, three additional assemblies for different program language support:
  • DX_CPPLanguage
  • DX_CSharpLanguage
  • DX_VBLanguage

With these assemblies we are able to parse CSharp, Visual Basic and C++ projects. They can be found here:

C:\Program Files (x86)\DevExpress 2011.1\IDETools\System\DXCore\BIN\SYSTEM

  1. .NET Framework assemblies:
  • Microsoft.Build.BuildEngine.dll
  1. And, finally, a couple of Visual Studio assemblies:
  • EnvDTE
  • VsLangProj

These two can be found in the "PublicAssemblies" folder:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\

Now, the DXCore support code. This code is required to load a solution, its projects and initialize DXCore parsers. I've added two folders:

  1. The Helpers folder contains the following classes:
  • LanguageHelper.cs - detects the language of projects (e.g. CSharp, Visual Basic or C++).
  • ParserHelper.cs - initializes DXCore parsers, and a few important DXCore services - the Source Model service and the Language service which are used to parse source code.
  • SolutionParser.cs - a helper class, which takes the path to the solution that you are going to parse. Calling the GetParsedSolution method will return the SolutionElement, which holds the abstract source tree of the entire solution.
  1. The Loaders folder contains the Visual Studio project and solution loaders for different Visual Studio versions. They are used to parse *.XXproj and *.sln files. There are versions for VS2002, VS2003 and VS2005. There are no dedicated loaders for VS2008 and VS2010, because those loaders for the old VS versions are perfectly fine to reading and loading newer Visual Studio project and solution format files (e.g. 2008, 2010).

Here's the final structure of the TestDXCoreConsoleApp:

DXCore ConsoleApp structure

The TestDXCoreConsoleApp with the full source is here (267,457 bytes, C#, VS2010), so you may review the code and use it as you'd like. Here's the Main function of the Program class:

static void Main(string[] args)
{
  string SolutionPath;
  if (args != null && args.Length > 0)
    SolutionPath = args[0];
  else
    SolutionPath = @"c:\Projects\TestDXCoreConsoleApp\TestDXCoreConsoleApp.sln";

  try
  {
    ParserHelper.RegisterParserServices();

    Console.Write("Parsing solution... ");

    SolutionParser solutionParser = new SolutionParser(SolutionPath);
    SolutionElement solution = solutionParser.GetParsedSolution();
    if (solution == null)
      return;

    Console.WriteLine("Done.");

    foreach (ProjectElement project in solution.AllProjects)
      foreach (SourceFile file in project.AllFiles)
        foreach (TypeDeclaration type in file.AllTypes)
        {
          Console.Write(type.FullName);
          Console.WriteLine(", members: " + ((ITypeElement)type).Members.Count);
        }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.Message);
  }
  finally
  {
    ParserHelper.UnRegisterParserServices();
  }

  Console.ReadLine();
}

If you put the sources into the "C:\Projects" folder and run the program without any arguments specified, you should see the following result:

DXCore Console App result

Press the Enter key to close the window. Bear in mind, that the parsing process may take some time, so you might need to wait a few seconds, until the entire solution is parsed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文