2014-12-30 36 views
0

我正在开发一个获取对象列表并将其导出为ex​​cel的类。使用泛型获取属性DisplayName表单资源类型

我正在使用EPPlus和泛型。 基本上解决的办法是:

  1. 创建一个泛型类获取对象的列表
  2. 使用的GetProperties
  3. 生成数据

我的问题是,以获得本地化的字符串生成的头为标题属性

我有一个类定义如下:

public class OrderToExport 
{ 
    [Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderID")] 
    public int OrderID { get; set; } 
    public string UserName { get; set; } 
    // Shipping Information 
    [Display(ResourceType = typeof(OrdersManagementStrings), Name = "ShippingType")] 
    public Order.eShippingType ShippingType { get; set; } 

    [Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderDate")] 
    public DateTime OrderDate { get; set; } 

    public string InstituteName { get; set; } 

    public decimal TotalPrice { get; set; } 

    [Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderType")] 
    public Order.eOrderType EOrderType { get; set; } 

    public string ShipingAddress { get; set; } 


    [Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderStatus")] 
    public Order.eOrderStatus EOrderStatus { get; set; } 

} 

我有一个方法:

public ExportToExcel(List<T> i_obj, string i_SheetName) 
    { 
foreach (var prop in typeof(T).GetProperties()) 
     { 
      //var n = prop.GetCustomAttributes(true).OfType<T>().Cast<T>(); 
      ws.Cells[row, column].Value = GetDisplayName(prop); 
      column++; 
     } 
} 

private string GetDisplayName(PropertyInfo property) 
    { 


     var attrName = GetAttributeDisplayName(property); 
     if (!string.IsNullOrEmpty(attrName)) 
      return attrName; 

     var metaName = GetMetaDisplayName(property); 
     if (!string.IsNullOrEmpty(metaName)) 
      return metaName; 

     return property.Name.ToString(); 
    } 

    private string GetAttributeDisplayName(PropertyInfo property) 
    { 
     var atts = property.GetCustomAttributes(true); 
     if (atts.Length == 0) 
      return null; 
     return (atts[0] as DisplayNameAttribute).DisplayName; 
    } 

    private string GetMetaDisplayName(PropertyInfo property) 
    { 
     var atts = property.DeclaringType.GetCustomAttributes(true); 
     if (atts.Length == 0) 
      return null; 

     var metaAttr = atts[0] as MetadataTypeAttribute; 
     var metaProperty = 
      metaAttr.MetadataClassType.GetProperty(property.Name); 
     if (metaProperty == null) 
      return null; 
     return GetAttributeDisplayName(metaProperty); 
    } 

现在,我不知道如何让存储在资源管理器中的刺(OrdersManagementStrings)该属性例如用于OrderID本地化属性。

任何帮助将不胜感激

+0

希望这会他lp you https://www.devexpress.com/Support/Center/Question/Details/A2820 – Avijit

+0

@Avijit不幸的是,它并没有帮助,因为我不知道如何从属性获取资源名称。没有资源名称和属性,我无法获得本地化的字符串 – Silagy

+1

行。这里是http://geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx – Avijit

回答

0

看一看这里:http://geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx 要了解如何改变这种文化在运行时使用,在链接中看到second comment

switch (comboBox1.Text) 
{ 
    case "neutral": 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(""); 
     break; 
    case "en-GB": 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB"); 
     break; 
    case "de-DE": 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE"); 
     break; 
} 

string messageText = Messages.MsgSampleText; 
MessageBox.Show(messageText);