迭代T4脚手架中的[ComplexType]属性?

时间:2017-03-20 18:22:19

标签: c# asp.net entity-framework metadata t4scaffolding

我正在定制用于Add => New Scaffolded Item ... =>“MVC 5 Controller with views,using Entity Framework”的实体框架ASP.NET CRUD模板。我还使用[ComplexType]属性和用于实体属性的类(例如,[ComplexType] FullName类用于我的Customer实体类中的SpouseFullName属性。)

public class Customer
{
    public string CustomerNumber { get; set; }
    public FullName SpouseFullName { get; set; } 
}

[ComplexType]
public class FullName
{
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public FullName()
    {
        FirstName = "";
        MiddleName = "";
        LastName = "";
    }
}

我希望能够在脚手架中迭代我的[ComplexType]中每个属性的属性元数据。例如:

<#
IEnumerable<PropertyMetadata> properties = ModelMetadata.Properties;
foreach (PropertyMetadata property in properties) 
{
    // Is this a [ComplexType]?
    if(property.IsComplexType)
    {
        // Iterate over metatdata here.
    }
}

理想情况下,我希望能够获得[ComplexType]中包含的IEnumerable<PropertyMetadata>个属性。想法?

1 个答案:

答案 0 :(得分:1)

想出来。我希望有更优雅的东西,但这很有效:

var element = document.getElementsByClassName("MagicZoomPlus")[0],
    path = element.getElementsByTagName('img')[0].getAttribute("src"),
    prefix = path.indexOf("/uploads")===0 ? "http://www.example.com" : "";
return prefix + path;

这使用反射加载复杂类型的程序集,不需要引用。完成后,您可以获取类型并迭代属性。