简体   繁体   English

T4模板获取属性

[英]T4 Template Get Attributes

I am trying to customise the T4 templates used in MVCScaffolding. 我正在尝试自定义MVCScaffolding中使用的T4模板。 I would like to filter out properties which are have the NotMapped attribute. NotMapped具有NotMapped属性的属性。

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. 我在2个月前运行相同的代码(它来自源代码控制,所以我确定),今天它失败了。
  • The same code when run from outside of T4 engine (eg MVC application) works perfectly fine. 从T4引擎(例如MVC应用程序)外部运行时相同的代码完全正常。

I have altered my code so it can be run from outside of Visual Studio: 我已经改变了我的代码,因此它可以从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 然后我使用TextTransform.exe而不是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" ). (您可以在以下位置找到TextTransform.exe:“C:\\ Program Files(x86)\\ Common Files \\ microsoft shared \\ TextTemplating \\ 10.0”)。

Also I have disabled hostspecific flag. 此外,我已禁用hosts特定标志。

The same template when executed from TextTransform.exe instead of VS worked perfectly. 从TextTransform.exe而不是VS执行时相同的模板工作得很好。 It seems that it's some kind of problem with VS. 看来这是VS的某种问题。 Please try using TextTransform. 请尝试使用TextTransform。 exe and tell if it works. exe并告诉它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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