Copy and clone in C #

Source: Internet
Author: User

1. Clone an object

 
Private object cloneobject (Object O) {type T = O. getType (); propertyinfo [] properties = T. getproperties (); object P = T. invokemember ("", system. reflection. bindingflags. createinstance, null, O, null); foreach (propertyinfo PI in properties) {If (Pi. canwrite) {object value = pi. getvalue (O, null); Pi. setvalue (p, value, null) ;}} return P ;}// callCodeIt is convenient to generate a new identical object. Example: textbox t = (textbox) cloneobject (textbox1 );

II,Method for cloning the dview specially

Public static datagridview clonedatagridview (datagridgdgv) {try {datagridview resultdgv = new datagridview dview (); resultdgv. columnheadersdefaultcellstyle = dgv. columnheadersdefaultcellstyle. clone (); datagridviewcellstyle dtgvdcs = dgv. rowsdefaultcellstyle. clone (); dtgvdcs. backcolor = dgv. defaultcellstyle. backcolor; dtgvdcs. forecolor = dgv. defaultcellstyle. forecolor; dtgvdcs. font = dgv. defaultcellstyle. font; resultdgv. rowsdefaultcellstyle = dtgvdcs; resultdgv. alternatingrowsdefaultcellstyle = dgv. alternatingrowsdefaultcellstyle. clone (); For (INT I = 0; I <dgv. columns. count; I ++) {maid = dgv. columns [I]. clone () as datagridviewcolumn; dtgvc. displayindex = dgv. columns [I]. displayindex; If (dtgvc. celltype = NULL) {dtgvc. celltemplate = new maid (); resultdgv. columns. add (dtgvc);} else {resultdgv. columns. add (dtgvc) ;}} foreach (maid VaR in dgv. rows) {maid row dtgvr = var. clone () as maid; dtgvr. defaultcellstyle = var. defaultcellstyle. clone (); For (INT I = 0; I <var. cells. count; I ++) {dtgvr. cells [I]. value = var. cells [I]. value;} If (var. index % 2 = 0) dtgvr. defaultcellstyle. backcolor = resultdgv. rowsdefaultcellstyle. backcolor; resultdgv. rows. add (dtgvr);} return resultdgv;} catch (exception ex) {MessageBox. show (ex. message);} return NULL ;}

3. Clone list

// Method 1: List <string> T = new list <string> (); // original list <string> T2 = new list <string> (T. toarray (); // copy of T // Method 2: it is a one liner using LINQ. list <string> list1 = new list <string> (); List <string> list2 = new list <string> (); // This will copy all the items from List 1 to list 2 list1.foreach (I => list2.add (I); // method 3: using system; using system. LINQ; using system. collections. generic; using system. diagnostics; namespace delegates {Class X {public int ID {Get; set;} public string name {Get; Set ;}} class y {public int ID {Get; set ;} public string name {Get; Set ;}} class program {static void main (string [] ARGs) {list <x> X = new list <x> (); for (INT I = 0; I <100; I ++) X. add (New X {id = I, name = string. format ("X _ {0}", I. tostring ()}); // copy X to Y list <Y> Y = new list <Y> (X. convertall <Y> (E => {return New Y {id = E. ID, name = E. name };})); debug. assert (X. count = y. count );}}}

reference Article : http://www.jiaonan. TV /html/blog/1/29606.htm

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.