Example #1
0
        /// <summary>
        /// 生成对应转换
        /// </summary>
        /// <typeparam name="TSource">源</typeparam>
        /// <typeparam name="TDest">目标</typeparam>
        /// <param name="mapType">转换方式</param>
        /// <param name="func">独立委托</param>
        /// <param name="key">单独命名独立委托缓存</param>
        public static void Bind <TSource, TDest>(MapType mapType = MapType.PTP, Action <TSource, TDest> func = null, string key = null)
        {
            string name = typeof(TSource).FullName + "To" + typeof(TDest).FullName + "_" + mapType;

            if (string.IsNullOrEmpty(key))
            {
                key = name;
            }
            FastInvoke fastInvok = FastInvokeCache.Instance.GetFastInvok(name, mapType);

            if (fastInvok != null)
            {
                return;
            }
            else
            {
                switch (mapType)
                {
                case MapType.PTP:
                    fastInvok = GetFastInvokPTP(typeof(TDest), typeof(TSource));
                    break;

                case MapType.PTPI:
                    fastInvok = GetFastInvokPTPI(typeof(TDest), typeof(TSource));
                    break;

                case MapType.FTF:
                    fastInvok = GetFastInvokFTF(typeof(TDest), typeof(TSource));
                    break;

                case MapType.FTFI:
                    fastInvok = GetFastInvokFTFI(typeof(TDest), typeof(TSource));
                    break;

                case MapType.PTF_TP:
                    fastInvok = GetFastInvokPTF_TP(typeof(TDest), typeof(TSource));
                    break;

                case MapType.FPTFP:
                    fastInvok = GetFastInvokFPTFP(typeof(TDest), typeof(TSource));
                    break;

                case MapType.FPTFPI:
                    fastInvok = GetFastInvokFPTFP(typeof(TDest), typeof(TSource));
                    break;

                default:
                    fastInvok = GetFastInvokPTP(typeof(TDest), typeof(TSource));
                    break;
                }
            }


            //执行成功的缓存
            FastInvokeCache.Instance.Add(name, mapType, fastInvok);
            if (func != null)
            {
                ActionInvoke <TSource, TDest> action = new ActionInvoke <TSource, TDest>
                {
                    Action = func
                };

                dicActions[key] = action;
            }
        }
Example #2
0
        /// <summary>
        /// 类型转换
        /// </summary>
        /// <typeparam name="TDest">目标</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="mapType">转换方式</param>
        /// <param name="func">执行委托</param>
        /// <param name="key">定义缓存名称</param>
        /// <returns></returns>
        public static TDest Mapper <TDest>(object obj, MapType mapType = MapType.PTP, Action <object, TDest> func = null, string key = null)

            where TDest : class, new()
        {
            string name = obj.GetType().FullName + "To" + typeof(TDest).FullName + "_" + mapType;

            if (string.IsNullOrEmpty(key))
            {
                key = name;
            }
            FastInvoke fastInvok = FastInvokeCache.Instance.GetFastInvok(name, mapType);

            if (fastInvok != null)
            {
                var    r      = (TDest)fastInvok.Invoke(obj);
                object action = null;
                if (dicActions.TryGetValue(key, out action))
                {
                    ActionInvoke <object, TDest> actionInvoke = (ActionInvoke <object, TDest>)action;
                    if (actionInvoke != null)
                    {
                        actionInvoke.Action.Invoke(obj, r);
                    }
                }
                return(r);
            }
            else
            {
                switch (mapType)
                {
                case MapType.PTP:
                    fastInvok = GetFastInvokPTP(typeof(TDest), obj.GetType());
                    break;

                case MapType.PTPI:
                    fastInvok = GetFastInvokPTPI(typeof(TDest), obj.GetType());
                    break;

                case MapType.FTF:
                    fastInvok = GetFastInvokFTF(typeof(TDest), obj.GetType());
                    break;

                case MapType.FTFI:
                    fastInvok = GetFastInvokFTFI(typeof(TDest), obj.GetType());
                    break;

                case MapType.PTF_TP:
                    fastInvok = GetFastInvokPTF_TP(typeof(TDest), obj.GetType());
                    break;

                case MapType.FPTFP:
                    fastInvok = GetFastInvokFPTFP(typeof(TDest), obj.GetType());
                    break;

                case MapType.FPTFPI:
                    fastInvok = GetFastInvokFPTFP(typeof(TDest), obj.GetType());
                    break;

                default:
                    fastInvok = GetFastInvokPTP(typeof(TDest), obj.GetType());
                    break;
                }
            }

            TDest result = (TDest)fastInvok.Invoke(obj);

            //执行成功的缓存
            FastInvokeCache.Instance.Add(name, mapType, fastInvok);
            if (func != null)
            {
                func(obj, result);
                ActionInvoke <object, TDest> action = new ActionInvoke <object, TDest>
                {
                    Action = func
                };

                dicActions[key] = action;
            }
            return(result);
        }