I'm editing the ASP.NET MVC Scaffold T4 templates and I need to get some extra class information. For example, , of the DisplayName
class attribute.
I found some examples:
var env = (DTE)((IServiceProvider)this.Host).GetService(typeof(EnvDTE.DTE));
var proj = env.Solution.Projects.OfType<Project>()
.Where(p => p.Name == assemblyName)
.FirstOrDefault();
var codeType = proj.CodeModel.CodeTypeFromFullName(classFullName);
var attr = codeType != null ? codeType.Attributes
.OfType<EnvDTE.CodeAttribute>()
.FirstOrDefault(x => x.Name == "DisplayName") : null;
var modelName = attr != null ?
attr.Value.Replace("\"", "") :
ViewDataTypeShortName;
So far so good. But I have in my Solution two projects that are from another Solution (and are in different Solution directories) but have been added normally via Add / Existing Project .
So these two other assemblies I can not get reference to using:
var proj = env.Solution.Projects.OfType<Project>()
.Where(p => p.Name == assemblyName)
.FirstOrDefault();
I even made a reflection on env.Solution.Projects.OfType<Project>()
to see what it would print:
<# foreach (var proj in env.Solution.Projects.OfType<Project>()) { #>
// <#= proj.Name #>
<# } #>
And I got something I did not understand. Among the projects that were actually created, such as:
// Common
// Projeto.Domain
// Projeto.Service
// Projeto.WebMVC
Where this Common
is a Solution Folder created for the other Solution projects. But the projects that are within it do not.
So another way I found to do a reflection in the assemblies was to add them directly:
<#@ assembly name="C:\Projects\SolutionA\Projeto.Domain\bin\Debug\Projeto.Domain.dll" #>
<#@ include namespace="Projeto.Domain.Entities" #>
<#@ assembly name="C:\Projects\SolutionA\Projeto.Service\bin\Debug\Projeto.Service.dll" #>
<#@ include namespace="Projeto.Service.ViewModels" #>
In this way reflection is already done in the traditional way.
The problem with this type of addition is that the fonts will not be in the same directories, which will result in the assemblies not being in the specified directories.
So I tried with, for example $(SolutionDir)
, but it seems that in Scaffold templates it does not work.
My question is how to add references logically or how to read the other assemblies via env.Solution.Projects
.
For those who have had experience, another way to solve the problem is also welcome!