Home > Code Samples > How to use DXCore in a Console App outside of Visual Studio

How to use DXCore in a Console App outside of Visual Studio

June 2nd, 2011

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 DXCore ConsoleApp

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:

DXCore ConsoleApp project properties

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

2) 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

3) .NET Framework assemblies:

  • Microsoft.Build.BuildEngine.dll

4) 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.

2) 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 attached (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 ConsoleApp results

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. Let me know if you have any difficulties with compiling the sources.

—–
Products: DXCore
Versions: 10.2 and up
VS IDEs: any
Updated: Jun/02/2011
ID: D088

Similar Posts:

  1. Przemyslaw Wlodarczak
    July 1st, 2011 at 15:46 | #1

    Awasome article. With your sources I was able to get nice framework for unit testing of my code issues.
    Thank you very much.

  2. July 4th, 2011 at 00:33 | #2

    @Przemyslaw Wlodarczak
    I am very glad that this has helped you!