zoukankan      html  css  js  c++  java
  • 深复制以及类型转换

     /// <summary>
            /// 深复制
            /// </summary>
            /// <param name="obj">请求对象</param>
            /// <returns>返回对象</returns>
            public static object CopyData(object obj)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    BinaryFormatter formatter = new BinaryFormatter();
    
                    formatter.Serialize(memoryStream, obj);
                    memoryStream.Position = 0;
    
                    return formatter.Deserialize(memoryStream);
                }
            }
    

      

       /// <summary>
            /// 复制实体类中相同类型并且相同名称的属性值
            /// </summary>
            /// <param name="inputEntity">输入实体</param>
            /// <param name="outputEntity">输出实体</param>
            public static void CopyData(object inputEntity, object outputEntity)
            {
                if (inputEntity == null || outputEntity == null)
                {
                    return;
                }
    
                Type inputEntityType = inputEntity.GetType();
                Type outputEntityType = outputEntity.GetType();
    
                foreach (PropertyInfo inputEntityProperty in inputEntityType.GetProperties())
                {
                    foreach (PropertyInfo outputEntityProperty in outputEntityType.GetProperties())
                    {
                        object inputValue = inputEntityProperty.GetValue(inputEntity, null);
    
                        if (inputValue != null
                            && inputEntityProperty.Name == outputEntityProperty.Name
                            && inputEntityProperty.PropertyType.Name == outputEntityProperty.PropertyType.Name)
                        {
                            outputEntityProperty.SetValue(outputEntity, inputValue, null);
                            break;
                        }
                    }
                }
            }
    

      

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.Collections;
    
    namespace Hotel.Product.Service.WebService.WebServiceCommon
    {
        /// <summary>
        /// 工具类
        /// </summary>
        public class Tool 
        {
            /// <summary>
            /// 类型转换,用于具有相同属性的类的转换
            /// </summary>
            /// <typeparam name="OldType">旧类型</typeparam>
            /// <typeparam name="NewType">新类型</typeparam>
            /// <param name="oldEntity">旧实体</param>
            /// <returns>新实体</returns>
            public static NewType TranslateEntity<OldType, NewType>(OldType oldEntity) 
            {
                NewType newEntity = Activator.CreateInstance<NewType>();
                Type t = typeof(NewType);
                Type oldType = typeof(OldType);
                foreach (PropertyInfo propertyInfo in t.GetProperties())
                {
                    if (oldType.GetProperty(propertyInfo.Name) == null)
                    {
                        continue;
                    }
                    object oldValue = oldType.GetProperty(propertyInfo.Name).GetValue(oldEntity, null);
                    object newValue = t.GetProperty(propertyInfo.Name).GetValue(newEntity, null);
                    if (oldValue != null && oldValue.GetType().Name == (new List<object>()).GetType().Name)
                    {
                        IList oldObjList = (IList)oldValue;
                        if(oldObjList.Count<0)
                        {
                            continue;
                        }
                        Type oldValueType = oldObjList[0].GetType();
                        Type newValueType = propertyInfo.PropertyType.GetGenericArguments()[0];
    
                        IList newObjList = (IList)newValue;
                        foreach (object item in oldObjList)
                        {
                            object newItem = TranslateDynamicsEntity(newValueType, item);
                            newObjList.Add(newItem);
                        }
                        newValue = newObjList;
                    }
                    else
                    {
                        newValue = oldType.GetProperty(propertyInfo.Name).GetValue(oldEntity, null);
                    }
                    propertyInfo.SetValue(newEntity, newValue, null);
                }
                return newEntity;
            }
            /// <summary>
            /// 类型转换
            /// </summary>
            /// <param name="newEntityType">新实体的类型</param>
            /// <param name="oldEntity">旧实体</param>
            /// <returns>新实体</returns>
            private static object TranslateDynamicsEntity(Type newEntityType, object oldEntity)
            {
                object newEntity = Assembly.GetAssembly(newEntityType).CreateInstance(newEntityType.FullName);
                Type newType = newEntity.GetType();
                Type oldType = oldEntity.GetType();
                foreach (PropertyInfo propertyInfo in newType.GetProperties())
                {
                    object oldValue = oldType.GetProperty(propertyInfo.Name).GetValue(oldEntity, null);
                    object newValue = newType.GetProperty(propertyInfo.Name).GetValue(newEntity, null);
                    if (oldValue != null && oldValue.GetType().Name == (new List<object>()).GetType().Name)
                    {
                        IList oldObjList = (IList)oldValue;
                        if (oldObjList.Count < 0)
                        {
                            continue;
                        }
                        Type oldValueType = oldObjList[0].GetType();
                        Type newValueType = propertyInfo.PropertyType.GetGenericArguments()[0];
    
                        IList newObjList = (IList)newValue;
                        foreach (object item in oldObjList)
                        {
                            object newItem = TranslateDynamicsEntity(newValueType, item);
                            newObjList.Add(newItem);
                        }
                        newValue = newObjList;
                    }
                    else
                    {
                        newValue = oldType.GetProperty(propertyInfo.Name).GetValue(oldEntity, null);
                    }
                    propertyInfo.SetValue(newEntity, newValue, null);
                }
                return newEntity;
            }
        }
    }
    

      

       /// <summary>
            /// 获取RateCode表一条数据
            /// </summary>
            /// <param name="rateCodeID">主键ID</param>
            /// <returns>RateCode实体</returns>
            internal static SOAEntity.RateCodeEntity GetDataById(int rateCodeID)
            {
                RateCodeEntity productEntity = ProductBusiness.RateCodeBusiness.HotelPubDB.GetDataById(rateCodeID);
                return Tool.TranslateEntity<RateCodeEntity, SOAEntity.RateCodeEntity>(productEntity);
            }
    

      

  • 相关阅读:
    8.C++-类的关键字
    BFS-九宫格重排(详解)
    7.C++类与封装的概念
    4.数码相框-freetype多行显示,居中显示
    3.数码相框-通过freetype库实现矢量显示
    Linux-iconv命令、并批处理将所有GBK文件转换为UTF8(18)
    2.数码相框-编码(ASCII/GB2312/Unicode)介绍,并使LCD显示汉字字符(2)
    1.数码相框-相框框架分析(1)
    Kotlin 之操作符重载
    Andorid-解决View重复点击的思路
  • 原文地址:https://www.cnblogs.com/songxiii/p/2391859.html
Copyright © 2011-2022 走看看