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. ******************************************************************* ------------------------------------------------------------------*/
您的资助是我最大的动力!
金额随意,欢迎来赏!
我写的东西能让你能懂,那是义务
毕竟占用了你生命中的宝贵的时间和注意力
要是你还能喜欢我的作品,那就是缘分了
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【青青子衿】!