EmitMapper的一些使用总结

/*----------------------------------------------------------------
 * 作    者 :姜  彦 
 * 项目名称 :Utility.Mapper.EmitMapper
 * 类 名 称 :EmitMapperEx 
 * 命名空间 :Utility.Mapper.EmitMapper
 * CLR 版本 :4.0.30319.42000
 * 创建时间 :2019/11/2 15:39:21
 * 当前版本 :1.0.0.0
 * WeChatQQ :771078740
 * My Email :jiangyan2008.521@gmail.com 
 *            jiangyan2008.521@qq.com  
 * 描述说明 : 
 * 
 * 修改历史 : 
 * 
*******************************************************************
 * Copyright @ JiangYan 2019. All rights reserved.
*******************************************************************
------------------------------------------------------------------*/

using EmitMapper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility.MapperHelper.EmitMapper
{
    /// <summary>
    /// EmitMapperEx
    /// </summary>
    public static class EmitMapperEx
    {

        /// <summary>
        /// 对象映射
        /// </summary>
        /// <typeparam name="S">源类型</typeparam>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="source">源对象</param>
        /// <returns></returns>
        public static T MapTo<S, T>(this S source)
        {
            ObjectsMapper<S, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<S, T>();
            return mapper.Map(source);
        }

        /// <summary>
        /// 对象列表映射
        /// </summary>
        /// <typeparam name="S">源类型</typeparam>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="sources">源对象列表</param>
        /// <returns></returns>
        public static IEnumerable<T> MapToList<S, T>(this IEnumerable sources)
        {
            ObjectsMapper<S, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<S, T>();          
            var list = mapper.MapEnum((IEnumerable<S>)sources);
            return list;
        }

        /// <summary>
        /// 对象列表映射
        /// </summary>
        /// <typeparam name="S">源类型</typeparam>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="sources">源对象列表</param>
        /// <returns></returns>
        [Obsolete("MapToList的前身,用的Foreach,效率不好", true)]
        public static List<T> SMapToList<S, T>(this IEnumerable sources)
        {
            ObjectsMapper<S, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<S, T>();
            var list = new List<T>();
            foreach (var item in sources)
            {
                list.Add(mapper.Map((S)item));
            }
            return list;
        }

        [Obsolete("EMapTo的前身,测试用", true)]
        private static T Convert<S, T>(S s)
        {
            ObjectsMapper<S, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<S, T>();
            return mapper.Map(s);
        }

        /// <summary>
        /// 对象映射
        /// </summary>
        /// <typeparam name="S">源类型</typeparam>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="source">源对象</param>
        /// <returns></returns>
        [Obsolete("为了不跟AutoMapper扩展方法重复而取得名字,可以启用", false)]
        public static T EMapTo<S, T>(this S source)
        {           
            ObjectsMapper<S, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<S, T>();
            return mapper.Map(source);
        }
        /// <summary>
        /// 对象列表映射
        /// </summary>
        /// <typeparam name="S">源类型</typeparam>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="sources">源对象列表</param>
        /// <returns></returns>
        [Obsolete("为了不跟AutoMapper扩展方法重复而取得名字,可以启用", false)]
        public static List<T> EMapToList<S,T>(this IEnumerable sources)
        {
            ObjectsMapper<S, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<S, T>();
            var list = new List<T>();
            foreach (var item in sources)
            {
                list.Add(mapper.Map((S)item));
            }
            return list;
        }
       
        [Obsolete("无法根据object类型有效的映射数据",false)]
        public static T TEMapTo<T>(this object obj)
        {
            if (obj == null) return default(T);            
            var t = obj.GetType();
            //var o = obj.Assembly.CreateInstance(t.Name);
            //var f = Activator.CreateInstance(t);
            ObjectsMapper<object, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<object, T>();
            return mapper.Map(obj);
        }
        [Obsolete("无法根据object类型有效的映射数据", false)]
        public static List<T> TEMapToList<T>(this IEnumerable source)
        {
            ObjectsMapper<object, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<object, T>();
            var list = new List<T>();
            foreach (var item in source)
            {
                T t = default(T);
                t = mapper.Map(item);
                list.Add(t);
                //list.Add(Convert<object,T>(item));
            }
            return list;
        }
        [Obsolete("无法根据object类型有效的映射数据", true)]
        private static T MMapTo<T>(this object obj)
        {
            if (obj == null) return default(T);
            var t = obj.GetType();
            //var o = obj.Assembly.CreateInstance(t.Name);
            //var f = Activator.CreateInstance(t);
            ObjectsMapper<object, T> mapper = ObjectMapperManager.DefaultInstance.GetMapper<object, T>();
            return mapper.Map(obj);
        }
    }




    /// <summary>
    /// https://www.cnblogs.com/xiangji/p/5456289.html
    /// </summary>
    public class EmitConventer
    {
        public EmitConventer()
        {
            _provider = ObjectMapperManager.DefaultInstance;
        }

        private ObjectMapperManager _provider;

        /// <summary>
        /// 功能转换提供者
        /// </summary>
        public ObjectMapperManager Provider
        {
            get { return _provider; }
            set { _provider = value; }
        }

        public T Convert<S, T>(S s)
        {
            ObjectsMapper<S, T> mapper = _provider.GetMapper<S, T>();
            return mapper.Map(s);
        }

        /// <summary>
        /// 是否可以转换 True:可以转化;False:不可以转换;
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="s"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool TryConvert<S, T>(S s, ref T t)
        {
            try
            {
                ObjectsMapper<S, T> mapper = _provider.GetMapper<S, T>();
                t = mapper.Map(s, t);
            }
            catch (Exception ex)
            {
                return false;
#if DEBUG
                //throw new Exception("", ex);
#endif
            }
            return true;

        }

    }

}

/*----------------------------------------------------------------
 * 备    注 :
 * 
 * 
 * 
*******************************************************************
 * Copyright @ JiangYan 2019. All rights reserved.
*******************************************************************
------------------------------------------------------------------*/

 

2.Profile文件

/*----------------------------------------------------------------
 * 作    者 :姜  彦 
 * 项目名称 :WPF.HOME
 * 类 名 称 :EmitMapperProfile 
 * 命名空间 :WPF.HOME
 * CLR 版本 :4.0.30319.42000
 * 创建时间 :2019/11/8 10:32:26
 * 当前版本 :1.0.0.0
 * WeChatQQ :771078740
 * My Email :jiangyan2008.521@gmail.com 
 *            jiangyan2008.521@qq.com  
 * 描述说明 : 
 * 
 * 修改历史 : 
 * 
*******************************************************************
 * Copyright @ JiangYan 2019. All rights reserved.
*******************************************************************
------------------------------------------------------------------*/

using EmitMapper;
using EmitMapper.MappingConfiguration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF.DTO.TreeView;
using WPF.Model.TreeView;

namespace WPF.HOME
{
    /// <summary>
    /// EmitMapperProfile
    /// </summary>
    public class EmitMapperProfile
    {
        /// <summary>
        /// 初始化EmitMapper配置
        /// </summary>
        public void Initialize()
        {
            IMappingConfigurator mappingConfigurator = new DefaultMapConfig().MatchMembers((x, y) =>
            {
                if (x == "Name" && y == "TestName")
                {
                    return true;
                }
                return x == y;
            });
            ObjectMapperManager.DefaultInstance.GetMapper<TreeViewDto, TreeViewModel>(mappingConfigurator);
        }
    }
}

/*----------------------------------------------------------------
 * 备    注 :
 * 
 * 
 * 
*******************************************************************
 * Copyright @ JiangYan 2019. All rights reserved.
*******************************************************************
------------------------------------------------------------------*/

 

posted @ 2019-11-24 15:17  <--青青子衿-->  阅读(534)  评论(0编辑  收藏  举报
// /**/ // 在页脚Html代码 引入 // function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);