In the past, when developing a system
People in the group are tired when writing methods. Many methods are object attributes that require a large number of repeated assignments and then added to the List object.
Are you still worried about having to manually write code and assign values to hundreds of attributes in an object?
The following is an example of reading database information.
[C-sharp]
View plaincopyprint?
- Public ilist <kfs_model.qas_v_messageinfo> pagertest (string where, kfs_model.pager P)
- {
- Sqldatareader DR = NULL;
- String table = "qas_v_messageinfo ";
- String orderby =
"Questiontime ";
- Ilist <kfs_model.qas_v_messageinfo> List =
New List <kfs_model.qas_v_messageinfo> ();
- Pagination. Page (table, where, orderby,
True, P );
- Dr = pagination. Page (table, where, orderby,
False, P );
- While (dr. Read ())
- {
- Kfs_model.qas_v_messageinfo QAS =
New kfs_model.qas_v_messageinfo ();
- QAS. userid = dr. getguid (1 );
- QAS. Username = dr. getstring (2 );
- QAS. sonmodelid = dr. getguid (3 );
- QAS. sonmodelname = dr. getstring (4 );
- QAS. questionid = dr. getguid (5 );
- QAS. questiontitle = dr. getstring (6 );
- QAS. questionurl = dr. getstring (7 );
- QAS. questiontime = dr. getdatetime (8 );
- QAS. questionsession = dr. getint32 (9 );
- QAS. questioncontent = dr. getstring (10 );
- QAS. fathermodelid = dr. getguid (11 );
- List. Add (QAS );
- }
- Return list;
- }
Public ilist <kfs_model.qas_v_messageinfo> pagertest (string where, kfs_model.pager p) <br/>{< br/> sqldatareader DR = NULL; <br/> string table = "qas_v_messageinfo "; <br/> string orderby = "questiontime"; <br/> ilist <kfs_model.qas_v_messageinfo> List = new list <kfs_model.qas_v_messageinfo> (); <br/> pagination. page (table, where, orderby, true, P); <br/> DR = pagination. page (table, where, orderby, false, P); <br/> while (dr. read () <br/>{< br/> kfs_model.qas_v_messageinfo QAS = new kfs_model.qas_v_messageinfo (); <br/> QAS. userid = dr. getguid (1); <br/> QAS. username = dr. getstring (2); <br/> QAS. sonmodelid = dr. getguid (3); <br/> QAS. sonmodelname = dr. getstring (4); <br/> QAS. questionid = dr. getguid (5); <br/> QAS. questiontitle = dr. getstring (6); <br/> QAS. questionurl = dr. getstring (7); <br/> QAS. questiontime = dr. getdatetime (8); <br/> QAS. questionsession = dr. getint32 (9); <br/> QAS. questioncontent = dr. getstring (10); <br/> QAS. fathermodelid = dr. getguid (11); <br/> list. add (QAS); <br/>}< br/> return list; <br/>}< br/>
The value assignment statement accounts for half of the method.
In addition, we need to compare them one by one.
Very depressing
The source code I published today does not have any copyright. You are welcome to reprint it.
First, construct a generic class.
Public class classname <t>
{
}
Define a method and method to return the set.
[C-sharp]
View plaincopyprint?
- Public class class1 <t>
- {
- Public ilist <t> getdata (sqldatareader reader)
- {
- Ilist <t> List = new list <t> ();
- Type type = typeof (t );
- Propertyinfo [] properties = type. getproperties ();
- While (reader. Read ())
- {
- T = activator. createinstance <t> ();
- For (INT I = 0; I <properties. length; I ++)
- {
- Properties [I]. setvalue (T, reader [I + 1],
Null );
- }
- List. Add (t );
- }
- Return list;
- }
- }
Public class class1 <t> <br/>{< br/> Public ilist <t> getdata (sqldatareader reader) <br/>{< br/> ilist <t> List = new list <t> (); <br/> type = typeof (t ); <br/> propertyinfo [] properties = type. getproperties (); </P> <p> while (reader. read () <br/>{< br/> T = activator. createinstance <t> (); <br/> for (INT I = 0; I <properties. length; I ++) <br/>{< br/> properties [I]. setvalue (T, reader [I + 1], null); </P> <p >}</P> <p> list. add (t); <br/>}</P> <p> return list; <br/>}< br/>}
The core code is given above. If you want to pass the SQL statement
This method is enough for your business logic layer!
The following extension method is provided by sql1234 in the Forum to lament the conciseness of the LINQ syntax.
[C-sharp]
View plaincopyprint?
- Public static ienumerable <t> getobjects <t> (this dbdatareader rd) where T:
New ()
- {
- VaR FS = (from FD in
Typeof (T). getfields ()
- Let DESC = new {field = FD, Index = RD. getordinal (FD. Name )}
- Where DESC. index> = 0
- Select DESC)
- . Tolist ();
- Foreach (var x in RD)
- {
- VaR OBJ = new T ();
- FS. foreach (d =>{ D. Field. setvalue (OBJ, RD [D. Index]) ;});
- Yield return OBJ;
- }
- }
Public static ienumerable <t> getobjects <t> (this dbdatareader rd) where T: New () <br/> {<br/> var FS = (from FD in typeof (t ). getfields () <br/> let DESC = new {field = FD, Index = RD. getordinal (FD. name)} <br/> where DESC. index> = 0 <br/> select DESC) <br/>. tolist (); <br/> foreach (var x in RD) <br/>{< br/> var OBJ = new T (); <br/> FS. foreach (D => {d. field. setvalue (OBJ, RD [D. index]) ;}); <br/> yield return OBJ; <br/>}</P> <p>
Here, we use the extension method to add a getobjects method to any dbdatareader to return a set of strongly typed objects of any specified type. Only private fields are complete. Modify getfields ()
Getfields (system. reflection. bindingflags. instance | system. reflection. bindingflags. Public | system. reflection. bindingflags. nonpublic)