• 快速反射DataTable


    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;
    }

  • 相关阅读:
    常见 Web 安全攻防总结
    传统方式接口测试返回值json验证
    Springboot中RestTemplate -- 用更优雅的方式发HTTP请求
    mock简单的json返回
    MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数
    MySQL数据库学习笔记(四)----MySQL聚合函数、控制流程函数(含navicat软件的介绍)
    MySQL数据库学习笔记(三)----基本的SQL语句
    MySQL数据库学习笔记(一)----MySQL 5.6.21的安装和配置(setup版)
    python实现广度优先搜索
    php递归
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5428849.html
Copyright © 2020-2023  润新知