C# 使用泛型获取属性DisplayName表单资源类型
我正在开发一个类,用于获取对象列表并将其导出到excel 我正在使用ePlus和泛型。 基本上,解决办法是: 创建获取对象列表的泛型类 使用GetProperties生成标题 生成数据 我的问题是获取header属性的本地化字符串 我有一个定义如下的类:C# 使用泛型获取属性DisplayName表单资源类型,c#,generics,resourcemanager,C#,Generics,Resourcemanager,我正在开发一个类,用于获取对象列表并将其导出到excel 我正在使用ePlus和泛型。 基本上,解决办法是: 创建获取对象列表的泛型类 使用GetProperties生成标题 生成数据 我的问题是获取header属性的本地化字符串 我有一个定义如下的类: public class OrderToExport { [Display(ResourceType = typeof(OrdersManagementStrings), Name = "OrderID")] public in
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);
}
现在,我不知道如何获取存储在资源管理器OrdersManagementString中的该属性的sting,例如OrderID的本地化属性
任何帮助都将不胜感激答案如下
感谢@Avijit在这里查看:
要了解如何更改运行时使用的区域性,请参阅链接中的:
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);
希望这将帮助您@Avijit不幸的是,它没有帮助,因为我不知道如何从属性获取资源名称。没有资源名称和属性,我无法获得本地化的stringOK。给你