zoukankan      html  css  js  c++  java
  • ASP.Net 反射显示

    //获取泛型T的类型
                        Type type = typeof(T);

                        //循环获取到的数据的行
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            //实例化获取到的类
                            object obj = Activator.CreateInstance(type);

                            //循环获取到的数据的列
                            for (int j = 0; j < dt.Columns.Count; j++)
                            {
                                //获取列的名称
                                PropertyInfo pinfo = type.GetProperty(dt.Columns[j].ColumnName);

                                #region 判断获取到的列的类型
                                //int类型
                                if (dt.Columns[j].DataType == typeof(Int32))
                                {
                                    //判断是否是空值
                                    if (dt.Rows[i][j] != null)
                                    {
                                        pinfo.SetValue(obj, int.Parse(dt.Rows[i][j].ToString(), null));
                                    }
                                    else
                                    {
                                        pinfo.SetValue(obj, 0, null);
                                    }
                                }

                                //float类型
                                if (dt.Columns[j].DataType == typeof(float))
                                {
                                    //判断是否是空值
                                    if (dt.Rows[i][j] != null)
                                    {
                                        pinfo.SetValue(obj, float.Parse(dt.Rows[i][j].ToString(), null));
                                    }
                                    else
                                    {
                                        pinfo.SetValue(obj, 0.0f, null);
                                    }
                                }

                                //datetime类型
                                if (dt.Columns[j].DataType == typeof(DateTime))
                                {
                                    //判断是否是空值
                                    if (dt.Rows[i][j] != null)
                                    {
                                        pinfo.SetValue(obj, DateTime.Parse(dt.Rows[i][j].ToString(), null));
                                    }
                                    else
                                    {
                                        pinfo.SetValue(obj, "", null);
                                    }
                                }

                                //double类型
                                if (dt.Columns[j].DataType == typeof(double))
                                {
                                    //判断是否是空值
                                    if (dt.Rows[i][j] != null)
                                    {
                                        pinfo.SetValue(obj, double.Parse(dt.Rows[i][j].ToString(), null));
                                    }
                                    else
                                    {
                                        pinfo.SetValue(obj, 0.00, null);
                                    }
                                }

                                //string类型
                                if (dt.Columns[j].DataType == typeof(string))
                                {
                                    //判断是否是空值
                                    if (dt.Rows[i][j] != null)
                                    {
                                        pinfo.SetValue(obj, dt.Rows[i][j].ToString(), null);
                                    }
                                    else
                                    {
                                        pinfo.SetValue(obj, "", null);
                                    }
                                }
                                #endregion
                            }

                            list.Add((T)obj);
                        }
                    }
                }
                return list;

  • 相关阅读:
    前端的一些雕虫小技,从100%和滚动条说起
    这事没完,继续聊spring cloud stream和kafka的这些小事
    简单聊一聊spring cloud stream和kafka的那点事
    是谁,在敲打我窗-CSS雨滴动画效果
    对照谈-官方spring-boot-starter和自定义starter异同分析
    从spring boot发邮件聊到开发的友好性
    抖音抖一抖-SVG和CSS视觉故障艺术小赏
    上位机组态控件PCHMI-PLC地址命名规则
    【C#格式化字符】【C#Chart图表控件】【饼图、柱形图、波形图】
    【PCHMI.DLL】【C#上位机二次开发文档】2021年7月15更新
  • 原文地址:https://www.cnblogs.com/XJNB/p/13150339.html
Copyright © 2011-2022 走看看