使用泛型获取属性显示名称表单资源类型

本文关键字:表单 资源 类型 显示 泛型 获取 属性 | 更新日期: 2023-09-27 18:35:39

我正在开发一个获取对象列表并将其导出到 excel 的类。

我正在使用 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);
    }

现在,我不知道如何获取该属性的资源管理器(订单管理字符串)中存储的刺痛,例如OrderID的本地化属性。

任何帮助将不胜感激

使用泛型获取属性显示名称表单资源类型

这是答案geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx

感谢@Avijit

看看这里: http://geekswithblogs.net/mapfel/archive/2008/11/01/126465.aspx若要了解如何更改要在运行时使用的区域性,请参阅链接中的第二条注释:

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