zoukankan      html  css  js  c++  java
  • C# Reflection PropertyInfo

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;
    using System.Reflection;


    namespace PropertyInfo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();

              
            }

        

            protected object AssembleObject(Type type, System.Data.IDataReader reader)
            {
                object o = Activator.CreateInstance(type);

                if (o == null)
                    return null;

               System.Reflection.PropertyInfo[] properties = type.GetProperties();//声明一个获取类型属性的变量

                for (int i = 0; i <reader.FieldCount; i++)
                {
                    System.Reflection.PropertyInfo property = type.GetProperty(reader.GetName(i));//得到第i列的所有属性
                    object val = reader[i];//第i列

                    if (val != DBNull.Value && property != null && property.CanWrite)//判断对象是否为空,属性是否为空,属性是否可写!如果都为true
                        property.SetValue(o, reader[i], null);//o对象,reader[i]对象的新值,索引器空的
                }

                return o;
            }


            public class User
            {
                private int _Id;

                public int Id
                {
                    get { return _Id; }
                    set { _Id = value; }
                }
                private string _Name;

                public string Name
                {
                    get { return _Name; }
                    set { _Name = value; }
                }
                private int _Sex;

                public int Sex
                {
                    get { return _Sex; }
                    set { _Sex = value; }
                }
                private string _SId;

                public string SId
                {
                    get { return _SId; }
                    set { _SId = value; }
                }
                private string _Pwd;

                public string Pwd
                {
                    get { return _Pwd; }
                    set { _Pwd = value; }
                }


            }

            private void button1_Click(object sender, EventArgs e)
            {
             
               
                DataTable dt = new DataTable();

                using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|db2.mdb;Persist Security Info=True"))
                {
                    string sql = @"select * from User";
                    conn.Open();

                    OleDbCommand cmd = new OleDbCommand("select * from db2.User", conn);

                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "dt");

                    dt = ds.Tables[0];

                    OleDbDataReader dr = cmd.ExecuteReader();

                    User u=new User();
                    while (dr.Read())
                    {
                        u = (User)AssembleObject(typeof(User), dr);
                    }

                }
            }


        }
    }

  • 相关阅读:
    Maven 安装 / 常用配置 / 阿里maven中央仓库
    记录:框架
    Mysql配置文件详解 my.cof
    Vmware虚拟机设置静态IP地址
    虚拟机安装centos发现inet为127.0.0.1,导致Xshell连接不上
    Servlet小案例总结
    Win10 提升系统响应速度
    从顶端插入,滚动展示(Demo):
    eclipse +cygwin+C++
    大型网站技术学习-3. 容器Docker与kubernetes
  • 原文地址:https://www.cnblogs.com/kingwangzhen/p/1688555.html
Copyright © 2011-2022 走看看