/// <summary> /// Add simple or complex item that maps source member to destination member. /// </summary> private Map AddItem <TSourceMember, TDestinationMember>(TSourceMember source, TDestinationMember destination, bool complex) where TSourceMember : MemberInfo where TDestinationMember : MemberInfo { if (source == null) { throw new ArgumentNullException("source"); } if (destination == null) { throw new ArgumentNullException("destination"); } var sourceItem = new MapItem.Item(source, true); var destinationItem = new MapItem.Item(destination, false); MapItem mapping = new MapItem(sourceItem, destinationItem, this); this.items.Add(mapping); if (complex) { Map subMap = MapResolver.Resolve(sourceItem.Type, destinationItem.Type); mapping.IsComplex = true; subMaps.Add(mapping.Key, subMap); } return(this); }
/// <summary> /// Add simple or complex item that maps source member to destination member. /// </summary> private Map AddItem(string sourceMemberName, Type sourceType, string destinationMemberName, Type destinationType, bool complex) { if (String.IsNullOrWhiteSpace(sourceMemberName)) { throw new ArgumentNullException("sourceMemberName"); } if (String.IsNullOrWhiteSpace(destinationMemberName)) { throw new ArgumentNullException("destinationMemberName"); } if (sourceType == null) { throw new ArgumentNullException("sourceType"); } if (destinationType == null) { throw new ArgumentNullException("destinationType"); } MemberInfo sm = sourceType.GetMember(sourceMemberName).FirstOrDefault(); MemberInfo dm = destinationType.GetMember(destinationMemberName).FirstOrDefault(); MapItem.Item sourceItem = null; MapItem.Item destinationItem = null; MapItem mapping = null; if (sm != null && dm != null) { sourceItem = new MapItem.Item(sm, true); destinationItem = new MapItem.Item(dm, false); mapping = new MapItem(sourceItem, destinationItem, this); this.items.Add(mapping); } if (mapping != null) { if (complex) { Map subMap = MapResolver.Resolve(sourceItem.Type, destinationItem.Type); mapping.IsComplex = true; subMaps.Add(mapping.Key, subMap); } } return(this); }
/// <summary> /// Sets destination object member value. /// </summary> private static void SetDestinationValue(MapItem.Item mapping, object destination, object value) { //// check whether to set property value or invoke method if (mapping.Member == MapItem.MemberType.Property) { ((PropertyInfo)mapping.MemberInfo).SetValue(destination, value, null); } else { //// expected set method has 1 parameter ((MethodInfo)mapping.MemberInfo).Invoke(destination, new object[] { value }); } }
/// <summary> /// Gets source object member value. /// </summary> /// <returns>A value returned by member.</returns> private static object GetSourceValue(MapItem.Item mapping, object source) { object value; //// check whether to get property value or invoke method if (mapping.Member == MapItem.MemberType.Property) { value = ((PropertyInfo)mapping.MemberInfo).GetValue(source, null); } else { //// expected get method has no parameters value = ((MethodInfo)mapping.MemberInfo).Invoke(source, null); } return(value); }