Open sandboxFocusImprove this doc

Inspecting a project using LINQPad

LINQPad is a widely used tool for interactively querying databases using Language Integrated Query (LINQ) syntax. It enables you to write and execute LINQ queries against various data sources, including SQL databases, XML documents, and .NET objects. With the Metalama driver for LINQPad, you can also query your source code as though it were a database.

Benefits

We developed the Metalama driver for LINQPad to assist developers and architects in building and testing their Metalama aspects and fabrics. However, this driver can be utilized even if you are not using Metalama.

With this driver, you can:

  • Test code queries using the same Metalama.Framework.Code API as the one used in aspects and fabrics,
  • List the target declarations of aspects,
  • Inspect the outcome of aspects, i.e., the output code transformations, diagnostics, or child aspects,
  • Query the resulting code mode.
Note

The Metalama.LinqPad driver is open-source.

Installing the driver

To install the Metalama driver for LINQPad, follow these steps:

  1. In the Explorer tool window, click Add connection.

    Install step 1

  2. Click on View more drivers.

    Install step 2

  3. In the NuGet LINQPad Manager dialog:

    1. Select Show all drivers
    2. Type Metalama
    3. Select Metalama.LinqPad and click Install.
    4. Accept a few disclaimers and wait. Then click Close.

    Install step 3

Opening a project or solution

  1. In the Explorer tool window, click Add connection.

    Install step 1

  2. Choose the Metalama driver and click Next.

    Add connection 1

  3. Specify the path to the C# project or solution, then click Ok.

    Add connection 2

Warning

The version of the Metalama.LinqPad driver must be higher or equal to the version of the Metalama.Framework package used in projects.

Data structure

Upon adding a C# project or solution to LINQPad, you should see the following structure:

Structure 1

The Workspace folder allows you to query the entire workspace in a single query, i.e., all projects for all target frameworks. Conversely, under the Projects folder, you see subfolders for individual projects, and queries will be limited to the selected combination of project and target framework.

Under the level of workspaces and projects, you see three subfolders:

  • Source code allows you to query the code before any transformation, i.e., literally, your source code.
  • Transformed code exposes the code after all transformations.
  • Aspects lists aspect classes, aspect instances, advice, and transformations.

For instance, the following query has been created from the Workspace folder. It lists all types in the workspace:

workspace.SourceCode.Types

This query, however, is limited to the Metalama.Samples.Cache1 project for the target framework net8.0.

workspace.GetProject("Metalama.Samples.Cache1", "net8.0").SourceCode.Types

The workspace object is of type Workspace, which implements the IProjectSet interface. The Project class represents a combination of project and target framework.

For details, see the Metalama.Framework.Workspaces namespace.

Querying Metalama Projects Without the Driver

Instead of using the driver and a connection, you can use the Metalama.LinqPad package from any query.

You should start your query with the MWorkspaceCollection class, then get the Default property and call the Load method to load your project or solution.

For instance, the following code defines the workspace variable and gives it an identical meaning to if you were using the driver:

var workspace = WorkspaceCollection.Default.Load(@"C:\src\Metalama.Samples\examples\log\log-3\Metalama.Samples.Log3.csproj")

To benefit from optimal rendering, add the following code to My Extensions:

public static class MyExtensions
{
	// Write custom extension methods here. They will be available to all queries.
 	public static object? ToDump(object obj) => Metalama.LinqPad.MetalamaDumper.ToDump(obj);
}

When you use the Metalama LinqPad driver or the MetalamaDumper class, objects that represent a code declaration have a virtual property called Permalink. The permalink is a C# expression that you can use to retrieve the declaration from a new query window.

Here is an example of a permalink that represents the _logger field of the LogAttribute class in the Metalama.Samples.Log4 project for the target framework net8.0.

workspace.GetDeclaration("Metalama.Samples.Log4", "net8.0", "F:LogAttribute._logger", false)

Querying Metalama projects without LinqPad

If you want to run a Metalama query from a different application, add a reference to the Metalama.Framework.Workspaces package.