0

I've created this question explaining the steps I took as a reference since my search took very long not knowing where and how to find this solution.

I'm creating a T4 template in my business Logic project, for creating some classes based on an existing class in and other project (same solution) as where my T4 Template is placed. I already loaded the VisualStudioHelper include from the tangible Template Gallery. This helped a lot for getting the entity class(es) from my Entity-Project.

Project targetProject= VisualStudioHelper.GetProject("ProjectName");

From There I got my class:

var allClasses = VisualStudioHelper.CodeModel.GetAllCodeElementsOfType(targetProject.CodeModel.CodeElements, EnvDTE.vsCMElement.vsCMElementClass, false);

CodeClass targetClass= allClasses
        .Cast<CodeClass>()
        .Where(p => p.Name == "ClassName")
        .Single();

Now I have my class where I can query my properties:

var allProperties = VisualStudioHelper.CodeModel.GetAllCodeElementsOfType(targetClass.Members, EnvDTE.vsCMElement.vsCMElementProperty, true);

I found out (obviously) that properties of the baseclass aren't registered as properties of the targeted CodeClass element.

Can someone help me out querying the baseclass properties for my T4 template.

As explained The T4 is in my Business Logic Layer (BLL), the entity classes that are targeted are in a common Entity project since I used EF-CodeFirst where my Entities are exactly the same in my BLL as in my DAL. The base Classes are coming from a Common Library project (Nuget) I wrote myself containing properties for every single Entity I use when using EF-CodeFirst.

Software Layering

Hope someone can help me.

Kind Regards, Luuk Krijnen

Luuk Krijnen
  • 1,180
  • 3
  • 14
  • 37

1 Answers1

0

I see you've got VisualStudioHelper, you may need add a method likes GetAllMethods in it.

Here is my code:

public IEnumerable<EnvDTE.CodeProperty> GetAllProperties(EnvDTE.CodeClass codeClass)
{
    var props = new List<EnvDTE.CodeProperty>();

    props.AddRange(GetProperties(codeClass));
    var baseClass = GetBaseClass(codeClass);
    if (baseClass != null)
        props.AddRange(GetAllProperties(baseClass));

    return props.Distinct(new CodePropertyEqualityComparer());
}

public IEnumerable<EnvDTE.CodeProperty> GetProperties(EnvDTE.CodeClass codeClass)
{
    return GetAllCodeElementsOfType(codeClass.Members, EnvDTE.vsCMElement.vsCMElementProperty, true).OfType<EnvDTE.CodeProperty>();
}
Morgan
  • 11
  • 3