C# 中的复制克隆 java程序员

一、克隆一个对象

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;
}
//调用代码生成新的一模一样的对象就很方便了,示例:
TextBox t =(TextBox)CloneObject(textBox1);

二、专门克隆DataGridView的方法
public static DataGridView CloneDataGridView(DataGridView dgv)
{
    try
    {
        DataGridView ResultDGV = new DataGridView();
        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++)
        {
            DataGridViewColumn DTGVC = dgv.Columns[i].Clone() as DataGridViewColumn;
            DTGVC.DisplayIndex = dgv.Columns[i].DisplayIndex;
            if (DTGVC.CellType == null)
            {
                DTGVC.CellTemplate = new DataGridViewTextBoxCell();
                ResultDGV.Columns.Add(DTGVC);
            }
            else
            {
                ResultDGV.Columns.Add(DTGVC);
            }
        }
        foreach (DataGridViewRow var in dgv.Rows)
        {
            DataGridViewRow Dtgvr = var.Clone() as DataGridViewRow;
            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;
}

三、克隆List
//方法一:
      List<string> t = new List<string>(); //original 
      List<string> t2 = new List<string>(t.ToArray()); // copy of t

//方法二:
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));

//方法三:
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);
    }

  }
}
参考文章:http://www.jiaonan.tv/html/blog/1/29606.htm

原文地址:https://www.cnblogs.com/java20130725/p/3215401.html