public void CombineWithOtherCustomMappings(CustomMapping root, IEnumerable <CustomMapping> mappings) { lock (root) { foreach (var otherMapping in mappings) { foreach (var m in otherMapping.Members) { if (!root.Members.Contains(m)) { var oldParams = otherMapping.ArgumentParameters.Select(p => p.Parameter).ToList(); var newParams = root.ArgumentParameters.Select(p => p.Parameter).ToList(); oldParams.Add(otherMapping.SourceParameter); newParams.Add(root.SourceParameter); var visitor = new ParameterVisitor(oldParams, newParams); var member = new MemberExpressionTuple { Expression = visitor.Visit(m.Expression), Member = m.Member }; root.Members.Add(member); } } } } }
private static CustomMapping GetCustomMappingFromNewExpression(Type destinationType, NewExpression expression) { var newMapping = new CustomMapping(); newMapping.DestinationType = destinationType; for (var i = 0; i < expression.Arguments.Count; i++) { var member = expression.Members[i]; var argument = expression.Arguments[i]; var memberOnDestination = destinationType.GetMember(member.Name).FirstOrDefault(); if (memberOnDestination == null) { throw new ArgumentException(string.Format("Member {0} does not exist on type {1}", member.Name, destinationType.Name)); } var memberExpression = new MemberExpressionTuple(); memberExpression.Member = memberOnDestination; if (argument is NewExpression) { var cm = GetCustomMappingFromNewExpression(memberExpression.Member.PropertyOrFieldType, (NewExpression)argument); if (cm != null) { newMapping.CustomMappings.Add(cm); } } else { memberExpression.Expression = argument; } newMapping.Members.Add(memberExpression); } return(newMapping); }
private static CustomMapping GetCustomMappingFromMemberInitExpression(Type destinationType, MemberInitExpression expression) { var newMapping = new CustomMapping(); newMapping.DestinationType = destinationType; foreach (MemberAssignment assignment in expression.Bindings) { var member = assignment.Member; var argument = assignment.Expression; var memberOnDestination = destinationType.GetMember(member.Name).FirstOrDefault(); if (memberOnDestination == null) { throw new ArgumentException(string.Format("Member {0} does not exist on type {1}", member.Name, destinationType.Name)); } var memberExpression = new MemberExpressionTuple(); memberExpression.Member = memberOnDestination; var argAsNewExpression = argument as NewExpression; if (argAsNewExpression != null && argAsNewExpression.Members != null) { newMapping.CustomMappings.Add(GetCustomMappingFromNewExpression(memberExpression.Member.PropertyOrFieldType, (NewExpression)argument)); } else { memberExpression.Expression = argument; } newMapping.Members.Add(memberExpression); } return(newMapping); }
private static CustomMapping GetCustomMappingFromNewExpression(Type destinationType, NewExpression expression) { var newMapping = new CustomMapping(); newMapping.DestinationType = destinationType; for (var i = 0; i < expression.Arguments.Count; i++) { var member = expression.Members[i]; var argument = expression.Arguments[i]; var memberOnDestination = destinationType.GetMember(member.Name).FirstOrDefault(); if (memberOnDestination == null) { throw new ArgumentException(string.Format("Member {0} does not exist on type {1}", member.Name, destinationType.Name)); } var memberExpression = new MemberExpressionTuple(); memberExpression.Member = memberOnDestination; if (argument is NewExpression) { newMapping.CustomMappings.Add(GetCustomMappingFromNewExpression(memberExpression.Member.PropertyOrFieldType, (NewExpression)argument)); } else { memberExpression.Expression = argument; } newMapping.Members.Add(memberExpression); } return newMapping; }