简体   繁体   中英

T4 Template Get Attributes

I am trying to customise the T4 templates used in MVCScaffolding. I would like to filter out properties which are have the NotMapped attribute.

My problem is that the attribute collection seems to be empty. I have tried printing out the attributes like below:

List<ModelProperty> properties = GetModelProperties(Model.ViewDataType, true);
foreach (ModelProperty property in properties) {
    if (!property.IsPrimaryKey && !property.IsForeignKey) {
#>
        <th>
            @Html.LabelFor(x => x.First().<#= property.Name #>)
            <!--
            <#foreach(var attribute in property.Type.CodeType.Attributes)
            {#>
              Attribute: <#=attribute.GetType().Name#>
            <#}#>
            -->
        </th>
<#
    }
}
#>

I cannot find any information about this which actually works and so far it is a very slow case of trial and error. What is the correct way to get the attributes or to get the template to ignore unmapped attributes?

It's not an exact answer to your question but it will provide more information about the problem. I have experienced exactly the same. There are some strange things about this issue:

  • It worked before. I run the same code (it's from source control so I'm sure) as 2 months ago and today it's failing.
  • The same code when run from outside of T4 engine (eg MVC application) works perfectly fine.

I have altered my code so it can be run from outside of Visual Studio:

string assemblyPath = Host.ResolveAssemblyReference("$(ProjectDir)$(OutDir)T4Mvc.dll");

was changed to:

string assemblyPath = @"D:\AbsolutePath\bin\T4Mvc.dll";

Then I used TextTransform.exe instead of RMB > Run Custom Tool

use

TextTransform.exe AjaxServices.tt -out AjaxServices.js

(You can find TextTransform.exe in: "C:\\Program Files (x86)\\Common Files\\microsoft shared\\TextTemplating\\10.0" ).

Also I have disabled hostspecific flag.

The same template when executed from TextTransform.exe instead of VS worked perfectly. It seems that it's some kind of problem with VS. Please try using TextTransform. exe and tell if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM