Dynamic filling of object classes through reflection

Source: Internet
Author: User

First: detects the entire entity class.

Using System;
Using System. Collections. Generic;
Using System. Data. sqlclient;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. reflection;
Using System. Data;

Public Partial Class Bangsotest_dr: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Ilist <user> userlist;
String Sqlcmd = " Select Top 1000 username, password from userinfo order by ID " ; // Here, username and password are automatically filled or skipped based on the corresponding fields in the object class.
Using (Sqldatareader DR = bangso. dbutility. sqlhelper. executedatareader (sqlcmd ))
{
Userlist = fillentityfromreader <user> (DR );
}

Foreach(User uInUserlist)
{
Response. Write (U. username +"<Br>");
}
}

Public Static Ilist <t> fillentityfromreader <t> (sqldatareader reader)
{
List <t> List = New List <t> ();
List <propertyinfo> pinfolist = New List <propertyinfo> ();
Boolean checkedpropertyinfo = False ;
While (Reader. Read ())
{
T instance = activator. createinstance <t> ();

# Region Check the matching of database fields and Object Attributes
If (! Checkedpropertyinfo)
{
Foreach (Propertyinfo pinfo In Typeof (T). getproperties ())
{
// Determine whether reader has the boundfieldattribute field in this property.
If (Reader. getschematable (). Select ( " Columnname =' " + Pinfo. Name +" ' " ). Length> 0 )
{
Pinfolist. Add (pinfo );
}
}
}
# Endregion

//Check complete
Checkedpropertyinfo =True;

// Start assignment
Foreach (Propertyinfo infoIn Pinfolist)
{
Int Index = reader. getordinal (info. Name );
If (Reader. getvalue (INDEX )! = Dbnull. value)
{
If (! Info. propertytype. isenum)
{
Info. setvalue (instance, convert. changetype (reader. getvalue (INDEX), info. propertytype ), Null );
}
Else
{
Info. setvalue (instance, enum. parse (info. propertytype, reader. getvalue (INDEX). tostring ()), Null );
}
}
}

list. add (instance);
}< br> reader. close ();
return list;
}

Public ClassUser
{
StringUsername;
Public StringUsername
{
Get{ReturnUsername ;}
Set{Username = value ;}
}
}
}

Second, select the attributes to be filled based on the characteristics in the object class.

Using System;
Using System. Collections. Generic;
Using System. Data. sqlclient;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. reflection;

Public Partial Class Bangsotest_dr: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
Ilist <user> userlist;
String Sqlcmd = " Select Top 100 username from userinfo " ;
Using (Sqldatareader DR = bangso. dbutility. sqlhelper. executedatareader (sqlcmd ))
{
Userlist = fillentityfromreader <user> (DR );
}

Foreach(User UIInUserlist)
{
Response. Write (ui. username +"<Br>");
}
}

Public Static Ilist <t> fillentityfromreader <t> (sqldatareader reader)
{
List <t> List = New List <t> ();
List <propertyinfo> pinfolist = New List <propertyinfo> ();
Boolean checkedpropertyinfo = False ;
While (Reader. Read ())
{
T instance = activator. createinstance <t> (); # Region Check the matching of database fields and Object Attributes

If (! Checkedpropertyinfo)
{
Foreach (Propertyinfo pinfo In Typeof (T). getproperties ())
{
Foreach (Boundfieldattribute ATTR In Pinfo. getcustomattributes ( Typeof (Boundfieldattribute ), True ))
{
// Determine whether reader has the boundfieldattribute field in this property.
If (Reader. getschematable (). Select ( " Columnname =' " + ATTR. fieldname + " ' " ). Length> 0 )
{
Pinfolist. Add (pinfo );
}
}
}
}
# Endregion

//Check complete
Checkedpropertyinfo =True;

// Start assignment
Foreach (Propertyinfo info In Pinfolist)
{
Int Index = reader. getordinal (info. Name );
If (Reader. getvalue (INDEX )! = Dbnull. value)
{
If (! Info. propertytype. isenum)
{
Info. setvalue (instance, convert. changetype (reader. getvalue (INDEX), info. propertytype ), Null );
}
Else
{
Info. setvalue (instance, enum. parse (info. propertytype, reader. getvalue (INDEX). tostring ()), Null );
}
}
}

List. Add (instance );
}
ReturnList;
}

Public ClassBoundfieldattribute: attribute
{
Private StringFieldname;

///   <Summary>
/// Name of the bound database Field
///   </Summary>
Public String Fieldname
{
Get { Return Fieldname ;}
}

///   <Summary>
/// Initialize a feature
///   </Summary>
///   <Param name = "_ fieldname"> Database field name </Param>
Public Boundfieldattribute ( String _ Fieldname)
{
This . Fieldname = _ fieldname;
}
}

Public Class User
{
String Username, password;
[Boundfieldattribute ( " Username " )] // The feature declaration here must be required, otherwise it will not be bound
Public String Username
{
Get { Return Username ;}
Set {Username = value ;}
}

Public String Password // The password here will not be filled because it has no declared features
{
Get { Return Password ;}
Set {Password = value ;}
}
}
}

 

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.