.Net 兩個對像之間的映射 ( 二 )

1、使用express

 

  class Program
    {
        static void Main(string[] args)
        {

            User u1 = new User();

            u1.UserName = "aaaaaaa";
            u1.Salary = 89.63m;
            u1.BirthDay = DateTime.Now;
            u1.Age = 88;


            User u2 = new ObjectMapping<User, User>().Mapping(u1);

            Console.WriteLine("U2.UserName="+u2.UserName);
            Console.WriteLine("U2.Age=" + u2.Age);
            Console.WriteLine("U2.Salary=" + u2.Salary);
            Console.WriteLine("U2.BirthDay=" + u2.BirthDay);

            Console.Read();

        }
    }

 

2、類app

 

/// <summary>
    /// 兩個對象之間的映射
    /// 此類未經優化,只是一個核心實現
    /// </summary>
    public class ObjectMapping<S, T> where S :class, new()  where T:class, new()
    {
        public T Mapping(S source) 
        {
            if (source == null)
            {
                return null;
            }

            T t = Activator.CreateInstance<T>();
         
            Type tType = t.GetType();
            Type sType = source.GetType();

            PropertyInfo[] proertyInfos = tType.GetProperties();

            if (!proertyInfos.Any())
            {
                return null;
            }

            foreach (var pro in  proertyInfos)
            {
                var sMethodInfo = sType.GetMethod("get_" + pro.Name);
                var tMethodInfo = tType.GetMethod("set_" + pro.Name);

                if (sMethodInfo == null || tMethodInfo == null)
                {
                    continue;
                }

                var sFunc = GetFuncByMethodInfo(sMethodInfo, source);
                var taction = GetActionByMethodInfo(tMethodInfo, t);

                taction.DynamicInvoke(sFunc.DynamicInvoke());
            }

            return t;
        }


        /// <summary>
        /// 獲取 一個方法的 FUNC
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Delegate GetFuncByMethodInfo(MethodInfo methodInfo,Object obj)
        {
            var parameters = new List<Type> ();

            parameters.AddRange(methodInfo.GetParameters().Select(p => p.ParameterType));

            parameters.Add(methodInfo.ReturnType);

            var funcType=  Expression.GetFuncType(parameters.ToArray<Type>());

            return  Delegate.CreateDelegate(funcType, obj, methodInfo);
        }



        /// <summary>
        /// 獲取一個方法的 ACTION
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Delegate GetActionByMethodInfo(MethodInfo methodInfo, Object obj)
        {
            var parameters = new List<Type>();

            parameters.AddRange(methodInfo.GetParameters().Select(p => p.ParameterType));

            var actionType = Expression.GetActionType(parameters.ToArray<Type>());

            return Delegate.CreateDelegate(actionType, obj, methodInfo);
        }


    }

 

 

相關知識:優化

 

Delegate.CreateDelegate 方法 

建立指定類型的委託。ui

命名空間:    System
程序集:  mscorlib(位於 mscorlib.dll)

重載列表
 
 
  名稱 說明
System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, MethodInfo)

建立指定類型的委託以表示指定的靜態方法。spa

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, MethodInfo, Boolean)

使用針對綁定失敗的指定行爲,建立用於表示指定靜態方法的指定類型的委託。3d

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Object, MethodInfo)

使用指定的第一個參數建立指定類型的委託,該委託表示指定的靜態方法或實例方法。code

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Object, MethodInfo, Boolean)

使用指定的第一個參數和針對綁定失敗的指定行爲,建立表示指定的靜態方法或實例方法的指定類型的委託。xml

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Object, String)

建立指定類型的委託,該委託表示要對指定的類實例調用的指定實例方法。對象

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Object, String, Boolean)

建立指定類型的委託,該委託表示要按指定的大小寫敏感度對指定類實例調用的指定實例方法。blog

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Object, String, Boolean, Boolean)

使用用於指定是否區分大小寫的值和針對綁定失敗的指定行爲,建立指定類型的委託,該委託表示要對指定類實例調用的指定實例方法。

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Type, String)

建立指定類型的委託,該委託表示指定類的指定靜態方法。

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Type, String, Boolean)

使用用於指定是否區分大小寫的值建立指定類型的委託,該委託表示指定類的指定靜態方法。

System_CAPS_pubmethodSystem_CAPS_static CreateDelegate(Type, Type, String, Boolean, Boolean)

使用用於指定是否區分大小寫的值和針對綁定失敗的指定行爲,建立指定類型的委託,該委託表示指定類的指定靜態方法。

 

 

Expression.TryGetFuncType 方法 (Type[], Type)

 

建立一個 Type 對象,它表示具備特定類型參數的泛型 System.Func 委託類型。 最後一個類型參數指定已建立委託的返回類型。

命名空間:    System.Linq.Expressions
程序集:  System.Core(位於 System.Core.dll)
相關文章
相關標籤/搜索