2014-01-28 59 views
2

我試圖訪問我的自定義屬性,如SortableAttribute附加到我正在編寫的T4控制器模板中的模型屬性。我已經複製了類塊,程序集並從List.tt導入爲樣板。在T4模板中訪問自定義屬性(VS2012 MVC4)

首先我想如下投的屬性:

<#+ 
string SortableBy(PropertyInfo property) 
{ 
    foreach (object attribute in property.GetCustomAttributes(true)) 
    { 
     var sortable = attribute as SortableAttribute; 
     if (sortable != null) return sortable.By == "" ? property.Name : sortable.By; 
    } 
    return property.Name; 
} 
#> 

然而,這沒有產生任何積極的結果,因爲我的項目的命名空間是未知的T4。爲了解決這個問題,我添加了我的項目的dll並導入了所需的名稱空間。

<#@ assembly name ="c:\path\to\MyProject\bin\MyProject.dll" #> 
<#@ import namespace= "MyProject.Filters" #> 

它似乎最初是成功的(例如,沒有錯誤的名稱空間導入),但它仍然沒有找到我的屬性。如果我將SortableAttribute替換爲MyProject.Filters.SortableAttribute,則錯誤消息是在MyProject.Filters中找不到SortableAttribute

爲了克服這個問題,我已經改變了我的代碼如下:

<#+ 
string SortableBy(PropertyInfo property) 
{ 
    foreach (object attribute in property.GetCustomAttributes(true)) 
    { 
     if (attribute != null && attribute.GetType().Name == "SortableAttribute") 
     { 
      var sortableBy = (string) attribute.GetType().GetProperty("By").GetValue(attribute, null); 
      return sortableBy == "" ? property.Name : sortableBy; 
     } 
    } 
    return property.Name; 
} 
#> 

我想我已經中獎,但我很快就意識到,property.GetCustomAttributes(true)返回所有屬性,但不是我的......

一個示例模型:

執行 SortableAttribute
public class MyModel 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    [Required] 
    [Display(Name = "Full Name")] 
    [Sortable] 
    public int Name { get; set; } 
} 

using System; 

namespace MyProject.Filters 
{ 
    [AttributeUsage(AttributeTargets.Property)] 
    public class SortableAttribute : Attribute 
    { 
     public SortableAttribute(string by = "") 
     { 
      this.By = by; 
     } 

     public string By { get; private set; } 
    } 
} 

請您指點我正確的方向?

任何幫助,非常感謝!

編輯:

事實證明,property.GetCustomAttributes(true)不會返回我的屬性,但是在SortableBy以下表達式始終計算爲null

(string) attribute.GetType().GetProperty("By").GetValue(attribute, null); 

知道爲什麼這可以嗎?

+0

顯示T4代碼你在哪裏得到的屬性FR OM的對象。我的猜測是你不檢查,你以爲你是性。因爲屬性名稱我得到正確 – Haney

+0

我檢查的權利屬性。請看我更新的問題。 –

回答

2

記住:構建修復了很多問題,但重建可以解決這一切!

總括來說,幫助別人寫T4模板,這裏是可以作爲樣板的一些工作代碼:

在讀自定義屬性。TT文件(在任何默認視圖模板基於腳手架()):

<#+ 
string SortableBy(PropertyInfo property) 
{ 
    foreach (object attribute in property.GetCustomAttributes(true)) 
    { 
     var sortable = attribute as SortableAttribute; 
     if (sortable != null) return sortable.By == "" ? property.Name : sortable.By; 
    } 
    return property.Name; 
} 
#> 

型號:

實施 SortableAttribute
public class MyModel 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 

    [Required] 
    [Display(Name = "Full Name")] 
    [Sortable] 
    public int Name { get; set; } 
} 

using System; 

namespace MyProject.Filters 
{ 
    [AttributeUsage(AttributeTargets.Property)] 
    public class SortableAttribute : Attribute 
    { 
     public SortableAttribute(string by = "") 
     { 
      this.By = by; 
     } 

     public string By { get; private set; } 
    } 
}