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

  • 相关阅读:
    nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】
    转 :hdoj 4857 逃生【反向拓扑】
    hdoj 3342 Legal or Not【拓扑排序】
    hdoj 2094 产生冠军
    poj 1789 Truck History【最小生成树prime】
    转:【拓扑排序详解】+【模板】
    hdoj 1285 确定比赛名次【拓扑排序】
    poj 2031 Building a Space Station【最小生成树prime】【模板题】
    zzuoj 10408: C.最少换乘【最短路dijkstra】
    [LC] 232. Implement Queue using Stacks
  • 原文地址:https://www.cnblogs.com/XJNB/p/13150339.html
Copyright © 2020-2023  润新知