public class SetterWrapper<TTarget, TValue>
{
private Action<TTarget, TValue> _setter;
public SetterWrapper(PropertyInfo propInfo)
{
if (propInfo == null)
throw new ArgumentNullException("propertyInfo");
if (!propInfo.CanWrite)
throw new NotSupportedException("属性是只读或Private Setter");
MethodInfo setMethod = propInfo.GetSetMethod(true);
_setter = (Action<TTarget, TValue>)Delegate.CreateDelegate(typeof(Action<TTarget, TValue>), null, setMethod);
}
public void SetValue(TTarget target, TValue val)
{
if (_setter != null)
{
_setter(target, val);
}
}
}
public class GetterWrapper<TTarget, TValue>
{
private Func<TTarget, TValue> _getter;
public GetterWrapper(PropertyInfo propInfo)
{
if (propInfo == null)
throw new ArgumentNullException("propertyInfo");
if (!propInfo.CanRead)
throw new NotSupportedException("属性是不可读或Private Getter");
MethodInfo getMethod = propInfo.GetGetMethod();
_getter = (Func<TTarget, TValue>)Delegate.CreateDelegate(typeof(Func<TTarget, TValue>), null, getMethod);
}
public TValue GetValue(TTarget target)
{
if (_getter != null)
{
return _getter(target);
}
return default(TValue);
}
}
public class DataConvertHelperEx
{
/// <summary>
/// 快速 Ilist<T> 转换成 DataTable
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable FastConvertToDataTable<T>(IList<T> i_objlist)
{
try
{
if (i_objlist == null || i_objlist.Count <= 0)
{
return null;
}
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties();//System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in i_objlist)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
//for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
foreach (var pi in myPropertyInfo)
{
//System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
//column = new DataColumn(name, pi.PropertyType);
column = new DataColumn(name);
dt.Columns.Add(column);
}
object val = null;
if (pi.PropertyType == typeof(string))
{
GetterWrapper<T, string> getter = new GetterWrapper<T, string>(pi);
val = getter.GetValue(t);
}
else if (pi.PropertyType == typeof(int))
{
GetterWrapper<T, int> getter = new GetterWrapper<T, int>(pi);
val = getter.GetValue(t);
}
else if (pi.PropertyType == typeof(float ))
{
GetterWrapper<T, float> getter = new GetterWrapper<T, float>(pi);
val = getter.GetValue(t);
}
else if (pi.PropertyType == typeof(DateTime))
{
GetterWrapper<T, DateTime> getter = new GetterWrapper<T, DateTime>(pi);
val = getter.GetValue(t);
}
else
{
val = pi.GetValue(t, null);
}
row[name] = val;// pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
catch(Exception ex)
{
}
return DataConvertHelper.ConvertToDataTable<T>(i_objlist);
}
}
public static DataTable ConvertToDataTable<T>(IList<T> i_objlist)
{
if (i_objlist == null || i_objlist.Count <= 0)
{
return null;
}
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (T t in i_objlist)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
{
System.Reflection.PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
//column = new DataColumn(name, pi.PropertyType);
column = new DataColumn(name);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}