Exemple #1
0
        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);
            }
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        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));
        }
Exemple #4
0
        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));
        }