/// <summary> /// 用source 更新target /// </summary> /// <typeparam name="TSource"></typeparam> /// <typeparam name="TTarget"></typeparam> /// <param name="entity"></param> /// <param name="target"></param> /// <param name="ignoreMembers"></param> /// <returns></returns> public static TTarget Map <TSource, TTarget>(TSource entity, TTarget target, params string[] ignoreMembers) where TTarget : new() { var config = new MapperConfig <TSource, TTarget>(); if (dictMap.ContainsKey(config)) { return(((Func <TSource, TTarget>)dictMap[config])(entity)); } foreach (var kv in dictMap.Where(d => d.Key.SourceType == config.SourceType && config.TargetType.IsAssignableFrom(d.Key.TargetType))) { return(((Func <TSource, TTarget>)kv.Value)(entity)); } return(DynamicMap(entity, target)); }
/// <summary> /// 注册对象类型转换器 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TResult"></typeparam> /// <param name="fn"></param> public static void CreateMap <T, TResult>(MapTo <T, TResult> fn) where TResult : new() { var config = new MapperConfig <T, TResult>(); lock (lockHelper) { if (!dictMap.ContainsKey(config)) { dictMap.Add(config, fn); } else { dictMap[config] = fn; } } }