Getting Custom Attributes in VS2013 T4 Scaffolding Templates

I am porting my previous MVC4.5 T4 Scaffolding templates to be used in VS2013. Everything is going well, fortunately, the logic of this has not changed much, but many namespaces, objects, and properties have been renamed as I expected.

However, a difficult bit PropertyInfo. It seems that PropertyInfoit is impossible to use , since the new object ModelMetadatacontains only PropertyMetadata. Since PropertyMetadatait has no method GetCustomAttributes()or similar, I am stuck in updating the following snippet:

<#+
string SearchableBy(PropertyInfo property) {
    foreach (object attribute in property.GetCustomAttributes(true))
    {
        var searchable = attribute as SearchableAttribute;
        if (searchable != null)
        {
            return searchable.By == "" ? GetValueExpressionSuffix(property) :
                                         searchable.By;
        }
    }
    return null;
}
#>
  • Is there any way to get an object PropertyInfoin the T4 Controller / View Scaffolder?
  • , / ? ModelMetadata

PS:


, VS2012, .

+4
2

. EnvDTE, T4. .

EnvDTE , EF DBContext ( MVC/WebAPI ). DBContext EnvDTE, , , EF ; T4 .

. , , DBContext. EF .

var env = (DTE)((IServiceProvider)this.Host).GetService(typeof(EnvDTE.DTE));
var project = dte.Solution.Projects.OfType<Project>().Where(p => p.Name == "your ef project name").FirstOrDefault();

CodeType codeType = project.CodeModel.CodeTypeFromFullName("your ef namespace.class");
CodeClass cc = (CodeClass)codeType;

List<CodeProperty> cps = cc.Members.OfType<CodeProperty>().ToList();

WriteLine(codeType.FullName);
WriteLine("======================");

foreach (CodeProperty cp in cps)
{
    WriteLine(cp.Name);
    foreach (CodeAttribute ca in cp.Attributes.OfType<CodeAttribute>())
    {
        WriteLine("-" + ca.Name);
    }
}

, , , EF EnvDTE.Project, . CodeClass , EF.

, ... , , . , , scaffolder, ModelMetadata , , .

, .

0

, : https://johniekarr.wordpress.com/2015/05/16/mvc-5-t4-templates-and-view-model-property-attributes/

namespace CustomViewTemplate
{
     [AttributeUsage(AttributeTargets.Property)]
     public class RichTextAttribute : Attribute
     {
         public RichTextAttribute() { }
     }
}

,

namespace CustomViewTemplate.Models
{     
     [Table("Person")]
     public class Person
     {
         [Key]
         public int PersonId { get; set;}

         [MaxLength(5)]
         public string Salutation { get; set; }

         [MaxLength(50)]
         public string FirstName { get; set; }

         [MaxLength(50)]
         public string LastName { get; set; }

         [MaxLength(50)]
         public string Title { get; set; }

         [DataType(DataType.EmailAddress)]
         [MaxLength(254)]
         public string EmailAddress { get; set; }

         [DataType(DataType.MultilineText)]
         [RichText] \\ Custom Attribute
         public string Biography { get; set; }     
     }
}

T4Helper,

using System; 

namespace CustomViewTemplate
{
     public static class T4Helpers
     {
         public static bool IsRichText(string viewDataTypeName, string propertyName)
         {
             bool isRichText = false;
             Attribute richText = null;
             Type typeModel = Type.GetType(viewDataTypeName);

             if (typeModel != null)
             {
                 richText = (RichTextAttribute)Attribute.GetCustomAttribute(typeModel.GetProperty(propertyName), typeof(RichTextAttribute));
                 return richText != null;
             }

             return isRichText;
         }
     }
}
+2

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


All Articles