访问自定义属性在T4模板(VS2012 MVC4) [英] Accessing Custom Attributes in a T4 Template (VS2012 MVC4)

查看:297
本文介绍了访问自定义属性在T4模板(VS2012 MVC4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问我的自定义属性,如 SortableAttribute 附着在T4控制器模板,我写我的模型属性。我复制了List.tt类块,组件和进口的样板。

I'm trying to access my custom attributes like SortableAttribute 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.

首先,我想投的属性如下:

First I tried to cast the 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;
}
#>

不过,这没有产生任何积极的结果,因为我的项目的命名空间是未知的T4。要解决这个问题,我已经加了我的项目的DLL和进口所需的命名空间。

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.

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

这似乎是成功的第一(例如没有在命名空间导入错误),但它依然没有找到我的属性。如果我换成 SortableAttribute MyProject.Filters.SortableAttribute ,该错误信息是, SortableAttribute 未在 MyProject.Filters 找到。

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 SortableAttribute with MyProject.Filters.SortableAttribute, the error message was that SortableAttribute was not found in MyProject.Filters.

要解决这个问题,我已经改变了我的code如下:

To overcome this, I've changed my code as follows:

<#+
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(真)将返回所有属性,但不是我的......

I thought I've hit the jackpot, but I soon had to realise that property.GetCustomAttributes(true) returns all attributes but not mine...

一个例子模型:

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

您可以请点我正确的方向?

Can you please point me to the right direction?

任何帮助是极大AP preciated!

Any help is greatly appreciated!

编辑:

原来, property.GetCustomAttributes(真)不会返回我的属性,但是在SortableBy下面前pression始终计算为

It turns out that property.GetCustomAttributes(true) does return my attributes, but the following expression in SortableBy always evaluates to null:

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

知道为什么这可以吗?

Any idea why this can be?

推荐答案

记住:建立修复了很多问题,但 REBUILD 可以解决这一切。

Remember: Build fixes a lot of problems, but REBUILD can fix it all!

要回顾一下,并帮助别人写T4模板,这里有一些工作code,它可以作为样板:

To recap and to help others writing T4 templates, here are some working code that can be used as boilerplate:

在.TT文件中读取自定义属性(基于脚手架()中的所有默认视图模板):

Reading custom attribute in a .tt file (based on Scaffold() in any default view template):

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

这篇关于访问自定义属性在T4模板(VS2012 MVC4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆