You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Rs.Framework
|
|
{
|
|
public class EntityHelper
|
|
{
|
|
/// <summary>
|
|
/// 实体类转换类
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="dr">需要转换的Dr</param>
|
|
/// <returns></returns>
|
|
public static T ToEntity<T>(DataRow dr) where T: new()
|
|
{
|
|
T entity = new T();
|
|
PropertyInfo[] pis = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
|
|
foreach (PropertyInfo propertyInfo in pis)
|
|
{
|
|
if(propertyInfo.PropertyType.IsEnum)
|
|
{
|
|
propertyInfo.SetValue(entity, Enum.Parse(propertyInfo.PropertyType, dr[propertyInfo.Name].ToString()));
|
|
}
|
|
else
|
|
{
|
|
propertyInfo.SetValue(entity, Convert.ChangeType(dr[propertyInfo.Name], propertyInfo.PropertyType));
|
|
}
|
|
}
|
|
return entity;
|
|
}
|
|
}
|
|
}
|