public void Compile(ModuleBuilder builder) { if (!TypeMapper <TSource, TTarget> .TryGetInstance(_container, out var mapper)) { var invokerBuilder = new ActionInvokerBuilder <TSource, TTarget>(_container.GetMapAction <TSource, TTarget>()); invokerBuilder.Compile(builder); var typeBuilder = builder.DefineStaticType(); var methodBuilder = typeBuilder.DefineStaticMethod("Invoke"); methodBuilder.SetParameters(typeof(IEnumerable <TSource>), typeof(IEnumerable <TTarget>)); var il = methodBuilder.GetILGenerator(); EmitMapper(il, invokerBuilder.MethodInfo, () => il.Emit(OpCodes.Ldarg_0), () => il.Emit(OpCodes.Ldarg_1)); il.Emit(OpCodes.Ret); #if NETSTANDARD var type = typeBuilder.CreateTypeInfo(); #else var type = typeBuilder.CreateType(); #endif _invokeMethod = type.GetMethod("Invoke"); } else if (mapper.MapperMethod == null) { mapper.CreateMapper(builder); } }
public void Compile(ModuleBuilder builder) { if (!_compiled) { Initialize(); _creator.Compile(builder); if (_beforeMapAction != null) { _beforeMapBuilder = new ActionInvokerBuilder <TSource, TTarget>(_beforeMapAction); _beforeMapBuilder.Compile(builder); } if (_customMapper != null) { _customInvokerBuilder = new ActionInvokerBuilder <TSource, TTarget>(_customMapper); _customInvokerBuilder.Compile(builder); } else { foreach (var mapper in _memberMappers) { mapper.Compile(builder); } } if (_afterMapAction != null) { _afterMapBuilder = new ActionInvokerBuilder <TSource, TTarget>(_afterMapAction); _afterMapBuilder.Compile(builder); } _compiled = true; } }
public void Compile(ModuleBuilder builder) { var invokerBuilder = new ActionInvokerBuilder <TSource, TTarget>(_container.GetMapAction <TSource, TTarget>()); invokerBuilder.Compile(builder); var typeBuilder = builder.DefineStaticType(); var methodBuilder = typeBuilder.DefineStaticMethod("Invoke"); methodBuilder.SetParameters(typeof(IEnumerable <TSource>), typeof(IEnumerable <TTarget>)); var il = methodBuilder.GetILGenerator(); var sourceEnumerator = il.DeclareLocal(typeof(IEnumerator <TSource>)); var targetEnumerator = il.DeclareLocal(typeof(IEnumerator <TTarget>)); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Callvirt, _sourceGetEnumeratorMethod); il.Emit(OpCodes.Stloc, sourceEnumerator); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Callvirt, _targetGetEnumeratorMethod); il.Emit(OpCodes.Stloc, targetEnumerator); var checkLabel = il.DefineLabel(); var startLabel = il.DefineLabel(); var endLabel = il.DefineLabel(); il.Emit(OpCodes.Br_S, checkLabel); il.MarkLabel(startLabel); il.Emit(OpCodes.Ldloc, sourceEnumerator); il.Emit(OpCodes.Callvirt, _sourceGetCurrentMethod); il.Emit(OpCodes.Ldloc, targetEnumerator); il.Emit(OpCodes.Callvirt, _targetGetCurrentMethod); il.Emit(OpCodes.Call, invokerBuilder.MethodInfo); il.MarkLabel(checkLabel); il.Emit(OpCodes.Ldloc, sourceEnumerator); il.Emit(OpCodes.Callvirt, _moveNextMethod); il.Emit(OpCodes.Brfalse_S, endLabel); il.Emit(OpCodes.Ldloc, targetEnumerator); il.Emit(OpCodes.Callvirt, _moveNextMethod); il.Emit(OpCodes.Brtrue_S, startLabel); il.MarkLabel(endLabel); il.Emit(OpCodes.Ret); #if NetCore var type = typeBuilder.CreateTypeInfo(); #else var type = typeBuilder.CreateType(); #endif _invokeMethod = type.GetMethod("Invoke"); }