2013-06-18 44 views
1

是否有人有一種方式來生成從模型項目中的數據字典?您的數據類型和顯示註釋以及字段名稱都在模型中,因此您可以使用此信息生成文本/ csv文件。構建數據字典從模型註釋

[Display(Name = "Type of Item")] 
public string Type { get; set; } 

看起來像這樣會是人們經常使用,如果它可用。

回答

0

使用反射,呼籲你的模型類Thing(),如循環需要得到的利息和加工性能參數。

public static void Thing<T>(this T model) where T : class 
{ 
    var t = typeof(T); 

    var props = 
    t.GetProperties() 
    .Select(p => new { p, attr = p.GetCustomAttributes(typeof(DisplayAttribute), false) }) 
    .Where(t1 => t1.attr.Length != 0) 
    .Select(t1 => t1.p).ToList(); 

    foreach (var prop in props) 
    { 
    // Do something 
    } 
}