首页 > 代码库 > 随便写了点反射用例

随便写了点反射用例

public static T[] ObjectsFieldNameChange<T>(T[] records)
{
if (records == null)
return records;
if (records.Count() <= 0)
return records;

var type = records.FirstOrDefault().GetType();
var listType = records.GetType();
var propertys = type.GetProperties();

if (propertys.Count() <= 0)
return records;

Dictionary<string, string> userDefineList = ClientCache.CacheCode.Where(q => q.fListName == UserDefineHeader).ToDictionary(q => q.fCodeName, q => q.fCodeValue);

if (userDefineList.Count() <= 0)
return records;

foreach (PropertyInfo property in propertys)
{
var userDefine = userDefineList.Where(q => q.Key.Contains(property.Name));
if (userDefine.Count() > 0)
{
var attributes = property.GetCustomAttributes(false);
for (int i = 0; i < attributes.Count(); i++)
{
if (attributes[i] is DisplayNameAttribute)
{
attributes[i] = new DisplayNameAttribute(userDefine.FirstOrDefault().Value);
}
}
}
}
return records;
}

 

随便写了点反射用例