zoukankan      html  css  js  c++  java
  • c# class copy class

    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);
        //    }
       
    }

  • 相关阅读:
    Paths on a Grid
    Three Kingdoms(优先队列+bfs)
    Factstone Benchmark(数学)
    C. Searching for Graph(cf)
    B. Trees in a Row(cf)
    String Successor(模拟)
    乘积最大的分解(数学)
    Kindergarten Election
    In 7-bit
    Friends
  • 原文地址:https://www.cnblogs.com/xsmhero/p/xsm.html
Copyright © 2011-2022 走看看