title: Accessing Custom Attributes in a T4 Template (VS2012 MVC4)
body: <p>I'm trying to access my custom attributes like <code>SortableAttribute</code> attached to my model properties in a T4 Controller template I'm writing. I've copied the class block, assemblies and imports from List.tt as boilerplate.</p>
  
  <p>First I tried to cast the attributes as follows:</p>
  
  <pre><code>&lt;#+
  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;
  }
  #&gt;
  </code></pre>
  
  <p>This however yielded no positive results since my project's namespace is unknown to T4. To resolve this, I have added my project's dll and imported the required namespace.</p>
  
  <pre><code>&lt;#@ assembly name ="c:\path\to\MyProject\bin\MyProject.dll" #&gt;
  &lt;#@ import namespace= "MyProject.Filters" #&gt;
  </code></pre>
  
  <p>It seemed to be successful at first (eg. no errors on the namespace import), but it still did not find my attributes. If I replaced <code>SortableAttribute</code> with <code>MyProject.Filters.SortableAttribute</code>, the error message was that <code>SortableAttribute</code> was not found in <code>MyProject.Filters</code>.</p>
  
  <p>To overcome this, I've changed my code as follows:</p>
  
  <pre><code>&lt;#+
  string SortableBy(PropertyInfo property)
  {
      foreach (object attribute in property.GetCustomAttributes(true))
      {
          if (attribute != null &amp;&amp; attribute.GetType().Name == "SortableAttribute")
          {
              var sortableBy = (string) attribute.GetType().GetProperty("By").GetValue(attribute, null);
              return sortableBy == "" ? property.Name : sortableBy;
          }
      }
      return property.Name;
  }
  #&gt;
  </code></pre>
  
  <p>I thought I've hit the jackpot, but I soon had to realise that <code>property.GetCustomAttributes(true)</code> returns all attributes but not mine...</p>
  
  <p>An example model:</p>
  
  <pre><code>public class MyModel
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      [Required]
      [Display(Name = "Full Name")]
      [Sortable]
      public int Name { get; set; }
  }
  </code></pre>
  
  <p>Implementation of <code>SortableAttribute</code>:</p>
  
  <pre><code>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; }
      }
  }
  </code></pre>
  
  <p>Can you please point me to the right direction?</p>
  
  <p>Any help is greatly appreciated!</p>
  
  <p><strong>Edit:</strong> </p>
  
  <p>It turns out that <code>property.GetCustomAttributes(true)</code> does return my attributes, but the following expression in SortableBy always evaluates to <code>null</code>:</p>
  
  <pre><code>(string) attribute.GetType().GetProperty("By").GetValue(attribute, null);
  </code></pre>
  
  <p>Any idea why this can be?</p>