private static void AddMapping(ILGenerator generator, Type sourceType, Type targetType, LocalBuilder sourceData, bool copy) { //Use if we have circular reference (slow reflection based) if (IsRecursiveMapping(sourceType, targetType, copy)) { //Get mapper generator.Emit(OpCodes.Ldtoken, sourceType); generator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); generator.Emit(OpCodes.Ldtoken, targetType); generator.Emit(OpCodes.Call, typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public)); generator.Emit(OpCodes.Ldloc, sourceData); generator.Emit(OpCodes.Ldarg_3); //Call map generator.EmitCall(OpCodes.Call, typeof(EmitMapperFactory).GetMethod(copy ? "Copy" : "Map", new Type[] { typeof(Type), typeof(Type), typeof(object), typeof(MappingContext) }), null); generator.Emit(OpCodes.Castclass, targetType); } //Fast without circular reference else { generator.Emit(OpCodes.Ldloc, sourceData); generator.Emit(OpCodes.Ldarg_3); var wMapperMethod = EmitMapperFactory.GetMapper(sourceType, targetType, copy).GetType().GetMethod("MapInstance", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy); generator.EmitCall(OpCodes.Call, wMapperMethod, null); } }
public static TTarget Copy <TSource, TTarget>(TSource source) where TSource : class where TTarget : class, new() { MappingContext wContext = new MappingContext(true); return(EmitMapperFactory.GetMapper <TSource, TTarget>(true).Map(source, null, wContext)); }
public static TTarget Map <TSource, TTarget>(TSource source, TTarget target) where TSource : class where TTarget : class, new() { MappingContext wContext = new MappingContext(false); return(EmitMapperFactory.GetMapper <TSource, TTarget>(false).Map(source, target, wContext)); }
public static TTarget Copy <TTarget>(object source) where TTarget : class, new() { if (source == null) { return(null); } MappingContext wContext = new MappingContext(true); return((TTarget)EmitMapperFactory.Copy(source.GetType(), typeof(TTarget), source, wContext)); }