public IList<T> ConvertDataTableToEntity<T>(DataTable dt) where T : class, new()
{
IList<T> list = new List<T>();
if (dt != null)
{
foreach (DataRow item in dt.Rows)
{
list.Add(FillEntity<T>(item));
}
}
return list;
}
public T FillEntity<T>(DataRow row) where T : class, new()
{
T obj = new T();
Type type = obj.GetType();
PropertyInfo[] props = type.GetProperties();
foreach (PropertyInfo prop in props)
{
//prop.GetCustomAttributes
if (prop.CanWrite && row.Table.Columns.Contains(prop.Name))
{
if (row[prop.Name] != null && row[prop.Name] != DBNull.Value)
{
SetValueCustom(obj, prop, row[prop.Name]);
}
}
}
return obj;
}
/// <summary>
/// 用索引属性的可选索引值设置该属性的值,自定义
/// </summary>
/// <param name="entity">需要赋值的对象</param>
/// <param name="prop">对象属性</param>
/// <param name="propValueStr">对象属性可选值</param>
public static void SetValueCustom(object entity, PropertyInfo prop, object propValueStr)
{
if (!prop.PropertyType.IsGenericType)
{
if (propValueStr != null)
{
if (prop.PropertyType == propValueStr.GetType())
{
prop.SetValue(entity, propValueStr, null);
}
else
{
object propValue = null;
if (prop.PropertyType.BaseType == typeof(System.Enum))
{
//判断
propValueStr = getEnumValue(prop.PropertyType, propValueStr.ToString());
}
if (prop.PropertyType.BaseType == typeof(System.Array) && propValueStr.GetType() == typeof(System.Array))
{
var jsonStr = getJsonArrayValue(prop.PropertyType, propValueStr.ToString());
propValue = JsonConvert.DeserializeObject(jsonStr, prop.PropertyType);
}
if (prop.PropertyType == typeof(System.Guid))
{
if (propValueStr == null)
{
propValue = null;
}
else
{
propValue = new Guid(propValueStr.ToString());
}
}
if (propValue == null)
{
propValue = Convert.ChangeType(propValueStr, prop.PropertyType);
}
prop.SetValue(entity, propValue, null);
}
}
}
else
{
//泛型Nullable<>
Type genericTypeDefinition = prop.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
prop.SetValue(entity, propValueStr == null ? null :
Convert.ChangeType(propValueStr, Nullable.GetUnderlyingType(prop.PropertyType)), null);
}
}
}
private static object getEnumValue(Type EnumType, string EnumValue)
{
int EnumValue_Int = -1;
string attrName = EnumValue;
if (int.TryParse(EnumValue, out EnumValue_Int))
{
attrName = Enum.GetName(EnumType, EnumValue_Int);
}
foreach (var myCode in Enum.GetValues(EnumType))
{
string attrVaule = myCode.ToString();//获取值
if (attrName.Equals(attrVaule, StringComparison.OrdinalIgnoreCase) || attrVaule.Equals(EnumValue, StringComparison.OrdinalIgnoreCase))
{
return myCode;
}
}
return null;
}
/// <summary>
/// 获取数组序列化字符串
/// </summary>
/// <param name="arrayType"></param>
/// <param name="ArrayValue"></param>
/// <returns></returns>
private static string getJsonArrayValue(Type arrayType, string ArrayValue)
{
if (string.IsNullOrEmpty(ArrayValue))
{
return "[]";
}
Type type = Type.GetType(arrayType.FullName.Replace("[]", ""), false, true);
Array arr = ArrayValue.Split(',').Where(c => !string.IsNullOrEmpty(c) && c.Trim().Length > 0).Select(c => Convert.ChangeType(c.Trim(), type)).ToArray();
return JsonConvert.SerializeObject(arr);
}
网友评论