Access to custom attributes in the T4 template (VS2012 MVC4)

I am trying to access my custom attributes, such as those SortableAttributeattached to my model properties in the T4 controller template that I am writing. I copied the class, assemblies and imports from List.tt as a template.

At first I tried to use attributes as follows:

<#+
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;
}
#>

However, this did not yield positive results, since the project namespace is unknown to T4. To solve this problem, I added the project DLL and imported the required namespace.

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

At first this seemed successful (e.g. errors in importing the namespace), but it still did not find my attributes. If I replaced SortableAttributewith MyProject.Filters.SortableAttribute, the error message was that SortableAttributeit was not found in MyProject.Filters.

, :

<#+
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) , ...

:

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

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

SortableAttribute:

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);

, ?

+2
2

: , REBUILD !

T4-, , :

.tt( Scaffold() ):

<#+
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;
}
#>

:

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

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

SortableAttribute:

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; }
    }
}
+2

Source: https://habr.com/ru/post/1544917/


All Articles