private static Type BuildActionCallerType(string typeName, MethodInfo mi) { var par = mi.GetParameters(); Type actionCallerType = null; if (par.Length == 0) { actionCallerType = typeof(MethodInvokerAction_0); } if (par.Length == 1) { actionCallerType = typeof(MethodInvokerAction_1); } if (par.Length == 2) { actionCallerType = typeof(MethodInvokerAction_2); } if (par.Length == 3) { actionCallerType = typeof(MethodInvokerAction_3); } else { new EmitMapperException("too many method parameters"); } var tb = DynamicAssemblyManager.DefineType(typeName, actionCallerType); MethodBuilder methodBuilder = tb.DefineMethod( "CallAction", MethodAttributes.Public | MethodAttributes.Virtual, null, Enumerable.Range(0, par.Length).Select(i => typeof(object)).ToArray() ); new AstComplexNode { nodes = new List <IAstNode> { CreateCallMethod(mi, par), new AstReturnVoid() } }.Compile(new CompilationContext(methodBuilder.GetILGenerator())); return(tb.CreateTypeInfo()); }
/// <summary> /// Creates an instance of Mapper for collections. /// </summary> /// <param name="MapperName">Mapper name. It is used for registration in Mappers repositories.</param> /// <param name="mapperMannager">Mappers manager</param> /// <param name="TypeFrom">Source type</param> /// <param name="TypeTo">Destination type</param> /// <param name="SubMapper"></param> /// <returns></returns> public static MapperForCollectionImpl CreateInstance( string MapperName, ObjectMapperManager mapperMannager, Type TypeFrom, Type TypeTo, ObjectsMapperDescr SubMapper, IMappingConfigurator mappingConfigurator ) { TypeBuilder tb = DynamicAssemblyManager.DefineType( "GenericListInv_" + MapperName, typeof(MapperForCollectionImpl) ); if (TypeTo.IsGenericType() && TypeTo.GetGenericTypeDefinition() == typeof(List <>)) { MethodBuilder methodBuilder = tb.DefineMethod( "CopyToListInvoke", MethodAttributes.Family | MethodAttributes.Virtual, typeof(object), new Type[] { typeof(IEnumerable) } ); InvokeCopyImpl(TypeTo, "CopyToList").Compile(new CompilationContext(methodBuilder.GetILGenerator())); methodBuilder = tb.DefineMethod( "CopyToListScalarInvoke", MethodAttributes.Family | MethodAttributes.Virtual, typeof(object), new Type[] { typeof(object) } ); InvokeCopyImpl(TypeTo, "CopyToListScalar").Compile( new CompilationContext(methodBuilder.GetILGenerator()) ); } MapperForCollectionImpl result = (MapperForCollectionImpl)Activator.CreateInstance(tb.CreateTypeInfo().AsType()); result.Initialize(mapperMannager, TypeFrom, TypeTo, mappingConfigurator, null); result.subMapper = SubMapper; return(result); }
private static Type BuildFuncCallerType(string typeName, MethodInfo mi) { var par = mi.GetParameters(); Type funcCallerType = null; if (par.Length == 0) { funcCallerType = typeof(MethodInvokerFunc_0); } if (par.Length == 1) { funcCallerType = typeof(MethodInvokerFunc_1); } if (par.Length == 2) { funcCallerType = typeof(MethodInvokerFunc_2); } if (par.Length == 3) { funcCallerType = typeof(MethodInvokerFunc_3); } else { new EmitMapperException("too many method parameters"); } var tb = DynamicAssemblyManager.DefineType(typeName, funcCallerType); MethodBuilder methodBuilder = tb.DefineMethod( "CallFunc", MethodAttributes.Public | MethodAttributes.Virtual, typeof(object), Enumerable.Range(0, par.Length).Select(i => typeof(object)).ToArray() ); new AstReturn { returnType = typeof(object), returnValue = CreateCallMethod(mi, par) }.Compile(new CompilationContext(methodBuilder.GetILGenerator())); return(tb.CreateTypeInfo()); }