Question

In our project we use a lot of T4 code generation. We use an ORM called DataObjects.Net of X-Tensive. We have a bit a chicken-egg problem. We need to compile the classes of the ORM before we can use the assembly's metadata to generate code. Then when the code is generated we can compile the model again but now in combination with generated code (partial classes). Most of the custom code in the model classes can not be compiled without the generated code.

How we solved it is to have a separate configuration in VS2013 called 'Model'. In this configuration a 'Conditional compilation symbol' call 'JUSTMODEL' is present. All code that does not compile without the generated code is placed between #if !JUSTMODEL #endif compiler directives which causes this code not be compiled for the 'Model' configuration.

This is a bit tedious. We have created a little extensiblity application that automates this proces but it more and more becomes pretty time consuming to run the proces.

Now I was wondering if it is possible to get the metadata of the Model assembly without building the assembly via an extensibility API of some kind. I see lots of tools in Visual Studio that understand classes loaded in the editors and give me some feedback (like intellisense).

Was it helpful?

Solution

There is an even better way than NRefactor. I've discovered the CodeModel inside Visual Studio. It gives me all the metadata I need to generate our code. You can find a lot of details on how to achieve that on the net like here:

  1. Microsoft
  2. Tangible

and many other places. The coding is a bit different than in C# because of the dynamic nature of the data but when you get the hang of it, it's quite easy and very powerfull.

OTHER TIPS

I would consider using NRefactory library or similar. The idea of NRefactory is similar to Roslyn so should solve you problem. If I understand you correctly you need information about classes and their members before compilation. The simplest code based on NRefactory, responsible for retrieving list of classes together with information about methods and properties, could look in the following way:

var parser = new CSharpParser();
var syntaxTree = parser.Parse(code);

var classes = syntaxTree.Descendants.OfType<TypeDeclaration>().Where(x => x.ClassType == ClassType.Class);
foreach (var typeDeclaration in classes)
{
    var result = typeDeclaration.Descendants.Where(d => d is MethodDeclaration || d is PropertyDeclaration);
    foreach (var declaration in result )
    {
        //...
    }
}

This example performs only syntax analysis of the source code. However, you can also perform semantic analysis with NRefactory by using CSharpAstResolver class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top