.Net 两个对像之间的映射 ( 二 )

一、使用

 

  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();

        }
    }

 

二、类

 

/// <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 方法 

创建指定类型的委托。

命名空间:    System
程序集:  mscorlib(位于 mscorlib.dll)

重载列表
 
 
 名称说明
System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, MethodInfo)

创建指定类型的委托以表示指定的静态方法。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, MethodInfo, Boolean)

使用针对绑定失败的指定行为,创建用于表示指定静态方法的指定类型的委托。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Object, MethodInfo)

使用指定的第一个参数创建指定类型的委托,该委托表示指定的静态方法或实例方法。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Object, MethodInfo, Boolean)

使用指定的第一个参数和针对绑定失败的指定行为,创建表示指定的静态方法或实例方法的指定类型的委托。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Object, String)

创建指定类型的委托,该委托表示要对指定的类实例调用的指定实例方法。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Object, String, Boolean)

创建指定类型的委托,该委托表示要按指定的大小写敏感度对指定类实例调用的指定实例方法。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Object, String, Boolean, Boolean)

使用用于指定是否区分大小写的值和针对绑定失败的指定行为,创建指定类型的委托,该委托表示要对指定类实例调用的指定实例方法。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Type, String)

创建指定类型的委托,该委托表示指定类的指定静态方法。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Type, String, Boolean)

使用用于指定是否区分大小写的值创建指定类型的委托,该委托表示指定类的指定静态方法。

System_CAPS_pubmethodSystem_CAPS_staticCreateDelegate(Type, Type, String, Boolean, Boolean)

使用用于指定是否区分大小写的值和针对绑定失败的指定行为,创建指定类型的委托,该委托表示指定类的指定静态方法。

 

 

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

 

创建一个 Type 对象,它表示具有特定类型参数的泛型 System.Func 委托类型。 最后一个类型参数指定已创建委托的返回类型。

命名空间:    System.Linq.Expressions
程序集:  System.Core(位于 System.Core.dll)

 

转载于:https://www.cnblogs.com/red-fox/p/6542689.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值