导航

C# Reflection PropertyInfo

Posted on 2010-03-17 23:25  kingwangzhen  阅读(524)  评论(0编辑  收藏  举报

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

            }
        }

 


    }
}