[Original] saves you the trouble of writing a lot of repeated code. Use propertyinfo class reflection to get the class type.

Source: Internet
Author: User

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?
  1. Public ilist <kfs_model.qas_v_messageinfo> pagertest (string where, kfs_model.pager P)
  2. {
  3. Sqldatareader DR = NULL;
  4. String table = "qas_v_messageinfo ";
  5. String orderby =
    "Questiontime ";
  6. Ilist <kfs_model.qas_v_messageinfo> List =
    New List <kfs_model.qas_v_messageinfo> ();
  7. Pagination. Page (table, where, orderby,
    True, P );
  8. Dr = pagination. Page (table, where, orderby,
    False, P );
  9. While (dr. Read ())
  10. {
  11. Kfs_model.qas_v_messageinfo QAS =
    New kfs_model.qas_v_messageinfo ();
  12. QAS. userid = dr. getguid (1 );
  13. QAS. Username = dr. getstring (2 );
  14. QAS. sonmodelid = dr. getguid (3 );
  15. QAS. sonmodelname = dr. getstring (4 );
  16. QAS. questionid = dr. getguid (5 );
  17. QAS. questiontitle = dr. getstring (6 );
  18. QAS. questionurl = dr. getstring (7 );
  19. QAS. questiontime = dr. getdatetime (8 );
  20. QAS. questionsession = dr. getint32 (9 );
  21. QAS. questioncontent = dr. getstring (10 );
  22. QAS. fathermodelid = dr. getguid (11 );
  23. List. Add (QAS );
  24. }
  25. Return list;
  26. }

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?
  1. Public class class1 <t>
  2. {
  3. Public ilist <t> getdata (sqldatareader reader)
  4. {
  5. Ilist <t> List = new list <t> ();
  6. Type type = typeof (t );
  7. Propertyinfo [] properties = type. getproperties ();
  8. While (reader. Read ())
  9. {
  10. T = activator. createinstance <t> ();
  11. For (INT I = 0; I <properties. length; I ++)
  12. {
  13. Properties [I]. setvalue (T, reader [I + 1],
    Null );
  14. }
  15. List. Add (t );
  16. }
  17. Return list;
  18. }
  19. }

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?
  1. Public static ienumerable <t> getobjects <t> (this dbdatareader rd) where T:
    New ()
  2. {
  3. VaR FS = (from FD in
    Typeof (T). getfields ()
  4. Let DESC = new {field = FD, Index = RD. getordinal (FD. Name )}
  5. Where DESC. index> = 0
  6. Select DESC)
  7. . Tolist ();
  8. Foreach (var x in RD)
  9. {
  10. VaR OBJ = new T ();
  11. FS. foreach (d =>{ D. Field. setvalue (OBJ, RD [D. Index]) ;});
  12. Yield return OBJ;
  13. }
  14. }

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)

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.