C # Reflection PropertyInfo

Source: Internet
Author: User

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, system. Data. idatareader reader)
{
Object o = activator. createinstance (type );

If (O = NULL)
Return NULL;

System. reflection. propertyinfo [] properties = type. getproperties (); // declare a variable for retrieving type attributes

For (INT I = 0; I <reader. fieldcount; I ++)
{
System. reflection. propertyinfo property = type. getproperty (reader. getname (I); // obtain all attributes of column I
Object val = reader [I]; // column I

If (Val! = Dbnull. Value & property! = NULL & property. canwrite) // determines whether the object is null, whether the attribute is null, and whether the attribute is writable! True
Property. setvalue (O, reader [I], null); // o object, new value of reader [I] object, blank Index
}

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

}
}

 

}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.