利用反射强制转换结构相同的类(类之间无需有继承关系)

using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;

namespace Common.Helpers
{
    /// <summary> 
    /// 类型转换器(利用反射) 
    /// </summary> 
    public static class TypeConverter
    {
        #region 相同结构的类的转换
        /// <summary> 
        /// 类型转换(相同结构的类的转换,如果当前类中的属性在源对象属性中不存在,则忽略该属性) 
        /// </summary> 
        /// <param name="arrSourceObjects">源对象数组</param> 
        /// <param name="toType">目标类型</param> 
        /// <returns>目标对象</returns> 
        public static List<Toutput> ConvertAll<Tinput, Toutput>(List<Tinput> arrSourceObjects) where Toutput : class
        {
            if (arrSourceObjects != null)
            {
                List<Toutput> list = new List<Toutput>();
                foreach (Tinput obj in arrSourceObjects)
                {
                    list.Add(Convert<Tinput, Toutput>(obj));
                }
                return list;
            }
            return null;
        }

        /// <summary> 
        /// 类型转换(相同结构的类的转换,如果当前类中的属性在源对象属性中不存在,则忽略该属性) 
        /// </summary> 
        /// <param name="sourceObject">源对象</param> 
        /// <param name="toType">目标类型</param> 
        /// <returns>目标对象</returns> 
        public static Toutput Convert<Tinput, Toutput>(Tinput sourceObject) where Toutput : class
        {
            #region 基础处理
            if (sourceObject == null)
            {
                return null;
            }

            Type sourceType = typeof(Tinput);
            Type toType = typeof(Toutput);

            //若为子类,直接返回源对象 
            Toutput returnObject = sourceObject as Toutput;
            if (returnObject != null)
            {
                return sourceObject as Toutput;
            }
            #endregion

            try
            {
                returnObject = Activator.CreateInstance(toType) as Toutput; //转换后的对象 
                PropertyInfo[] targetObjProperties = toType.GetProperties(); //目标对象的属性信息 

                foreach (PropertyInfo objProperty in targetObjProperties)
                {
                    //获取源对象对应属性的值,赋予新对象(当两个属性的类型一致或可转化时赋值) 
                    PropertyInfo sourcePropertyInfo = sourceType.GetProperty(objProperty.Name);
                    if (sourcePropertyInfo != null && objProperty.PropertyType.IsAssignableFrom(sourcePropertyInfo.PropertyType))
                    {
                        object objSourceValue = sourcePropertyInfo.GetValue(sourceObject, null);
                        objProperty.SetValue(returnObject, objSourceValue, null);
                    }
                }

                return returnObject;
            }
            catch (Exception ex)
            {
                string strMsg = string.Format("源类型{0}转换成目标类型{1}时失败,失败原因:{2}", sourceType, toType, ex.Message);
                throw new ApplicationException(strMsg, ex);
            }
        }
        #endregion
    }
}

泛型方法,实现类型之间的强制转换(将属性赋值给另一个类中同名的属性),可用于两台服务器互相引用服务时,虽然反射的效率不高,但是个人感觉在程序中,一般来说瓶颈都在数据库,不会存在于代码中。

源文件下载:https://files.cnblogs.com/feng8621/TypeConverter.rar

原文地址:https://www.cnblogs.com/feng8621/p/2724903.html