我有这个元数据:
public class ForecastVSPurchaseUploadedPOMasterItemMetadata
{
[Display(Name ="Forecast")]
public decimal m;
}
和部分类:
[MetadataType(typeof(ForecastVSPurchaseUploadedPOMasterItemMetadata))]
public partial class V_CT2_ForecastVSPurchaseUploadedPOMasterItem
{
}
班级:
public partial class V_CT2_ForecastVSPurchaseUploadedPOMasterItem
{
public decimal m { get; set; }
}
我试图通过下面的代码(扩展方法)获取名称:
var customAttribute = memberInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
if (customAttribute != null)
{
titleName = (customAttribute as DisplayAttribute).Name;
}
else
{
titleName = memberInfo.Name;
}
但customAttribute
始终为空。
我是根据此question
执行此操作的我使用@Html.DisplayNameFor(model => model.m)
进行了测试,它会显示我在元数据类中设置的任何名称。
元数据metaProperty
的属性也为空:
public static List<string> GetPropertyDisplayNames(this Type type)
{
var titleList = new List<string>();
var propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
var titleName = propertyInfo.GetDisplayName();
{
var atts = propertyInfo.DeclaringType.GetCustomAttributes(typeof(MetadataTypeAttribute), true);
if (atts != null)
{
var metaAttr = atts[0] as MetadataTypeAttribute;
var metaProperty = metaAttr.MetadataClassType.GetProperty(propertyInfo.Name);
}
}
titleList.Add(titleName);
}
return titleList;
}
扩展类代码:
public static class EnumerableExtensionClass
{
public static HSSFWorkbook ExportExcel<T>(this IEnumerable<T> dataList)
{
//Create workbook
var datatype = typeof(T);
var workbook = new HSSFWorkbook();
var worksheet = workbook.CreateSheet(string.Format("{0}", datatype.GetDisplayName()));
//Insert titles
var row = worksheet.CreateRow(0);
var titleList = datatype.GetPropertyDisplayNames();
for (int i = 0; i < titleList.Count; i++)
{
row.CreateCell(i).SetCellValue(titleList[i]);
}
//Insert data values
for (int i = 1; i < dataList.Count() + 1; i++)
{
var tmpRow = worksheet.CreateRow(i);
var valueList = dataList.ElementAt(i - 1).GetPropertyValues();
for (int j = 0; j < valueList.Count; j++)
{
tmpRow.CreateCell(j).SetCellValue(valueList[j]);
}
}
//Save file
return workbook;
}
public static string GetDisplayName(this MemberInfo memberInfo)
{
var titleName = string.Empty;
//Try get DisplayName
var attribute = memberInfo.GetCustomAttributes(typeof(DisplayNameAttribute), false).FirstOrDefault();
if (attribute != null)
{
titleName = (attribute as DisplayNameAttribute).DisplayName;
}
//If no DisplayName
else
{
var customAttribute = memberInfo.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault();
if (customAttribute != null)
{
titleName = (customAttribute as DisplayAttribute).Name;
}
else
{
titleName = memberInfo.Name;
}
}
return titleName;
}
public static List<string> GetPropertyDisplayNames(this Type type)
{
var titleList = new List<string>();
var propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
var titleName = propertyInfo.GetDisplayName();
titleList.Add(titleName);
}
return titleList;
}
public static List<string> GetPropertyValues<T>(this T data)
{
var propertyValues = new List<string>();
var propertyInfos = data.GetType().GetProperties();
foreach (var propertyInfo in propertyInfos)
{
propertyValues.Add(propertyInfo.GetValue(data, null).ToString());
}
return propertyValues;
}
}