c# class copy class

来源:互联网 发布:static java 编辑:程序博客网 时间:2024/04/30 17:07

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Text;
using ChoosesClass.Data;
using ChoosesClass.Entities;
using ChoosesClass.Data.SqlClient;
using System.Reflection;

/// <summary>
///  WSTableToTable 的摘要说明
///
/// TableClass tableClass = DataRepository.BoxProvider.GetByFBoxId(1);
/// TableClass2 tableClass2 = new BoxServer();        
/// CopyClass(tableClass2, tableClass);
/// tableClass2=(tableClass2) CopyClass(tableClass2, tableClass);
/// </summary>
public class TableClassToTableClass
{
    /// <summary>
    /// 拷贝类的方法
    /// </summary>
    /// <param name="class1">要拷贝的类</param>
    /// <param name="class2">被拷贝的类(只能是orm的表生成的类)</param>
     // public static object CopyTableClass(object tableClass, object tableClass2)
    public static void CopyTableClass(object tableClass, object tableClass2)
    {
        //得到class的类型
        Type typeTableClass = tableClass.GetType();
        Type typeTableClass2 = tableClass2.GetType();
        //得到class2的基元类型
        PropertyInfo cloumn = typeTableClass2.GetProperty("TableColumns");
        //得到所有的字段名称
        string[] list = (string[])cloumn.GetValue(tableClass2, null);
        foreach (object item in list)
        {

            PropertyInfo proTableClass = typeTableClass.GetProperty(item.ToString());
            PropertyInfo proTableClass2 = typeTableClass2.GetProperty(item.ToString());
            //给class1的每个字段赋值
            proTableClass.SetValue(tableClass, proTableClass2.GetValue(tableClass2, null), null);
        }
        //return tableClass;
    }
    //    TableClass tableClass = DataRepository.BoxProvider.GetByFBoxId(1);
    //    TableClass2 tableClass2 = new BoxServer();        
    //    CopyClass(tableClass2, tableClass);           
    //    tableClass2=(tableClass2) CopyClass(tableClass2, tableClass);

  
   
    //TList<table> tListBox = DataRepository.BoxProvider.GetAll();


    //    List<BoxServer> listBoxServer = new List<BoxServer>();

    //    Array list = Enum.GetValues(typeof(BoxColumn));

    //    foreach (Box item in tListBox)
    //    {
    //        BoxServer boxServer = new BoxServer();
    //        foreach (BoxColumn item1 in list)
    //        {
    //            Type typeBox = item.GetType();
    //            Type typeBoxServer = boxServer.GetType();

    //            PropertyInfo probox = typeBox.GetProperty(item1.ToString());
    //            PropertyInfo proboxServer = typeBoxServer.GetProperty(item1.ToString());

    //            proboxServer.SetValue(boxServer, probox.GetValue(item, null), null);
    //        }
    //        listBoxServer.Add(boxServer);
    //    }
   
}