/// <summary> /// Add simple or complex item that maps source member to destination member. /// </summary> /// <typeparam name="TSourceMember"></typeparam> /// <typeparam name="TDestinationMember"></typeparam> /// <param name="source">The source<see cref="TSourceMember"/></param> /// <param name="destination">The destination<see cref="TDestinationMember"/></param> /// <param name="complex">The complex<see cref="bool"/></param> /// <returns>The <see cref="SimpleMapper"/></returns> private SimpleMapper AddItem <TSourceMember, TDestinationMember>(TSourceMember source, TDestinationMember destination, bool complex) where TSourceMember : MemberInfo where TDestinationMember : MemberInfo { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (destination == null) { throw new ArgumentNullException(nameof(destination)); } var sourceItem = new MappedItem.Item(source, true); var destinationItem = new MappedItem.Item(destination, false); MappedItem mapping = new MappedItem(sourceItem, destinationItem, this); this.items.Add(mapping); if (complex) { SimpleMapper 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> /// <param name="sourceMemberName">The sourceMemberName<see cref="string"/></param> /// <param name="sourceType">The sourceType<see cref="Type"/></param> /// <param name="destinationMemberName">The destinationMemberName<see cref="string"/></param> /// <param name="destinationType">The destinationType<see cref="Type"/></param> /// <param name="complex">The complex<see cref="bool"/></param> /// <returns>The <see cref="SimpleMapper"/></returns> private SimpleMapper AddItem(string sourceMemberName, Type sourceType, string destinationMemberName, Type destinationType, bool complex) { if (string.IsNullOrWhiteSpace(sourceMemberName)) { throw new ArgumentNullException(nameof(sourceMemberName)); } if (string.IsNullOrWhiteSpace(destinationMemberName)) { throw new ArgumentNullException(nameof(destinationMemberName)); } if (sourceType == null) { throw new ArgumentNullException(nameof(sourceType)); } if (destinationType == null) { throw new ArgumentNullException(nameof(destinationType)); } MemberInfo sm = sourceType.GetMember(sourceMemberName).FirstOrDefault(); MemberInfo dm = destinationType.GetMember(destinationMemberName).FirstOrDefault(); MappedItem.Item sourceItem = null; MappedItem.Item destinationItem = null; MappedItem mapping = null; if (sm != null && dm != null) { sourceItem = new MappedItem.Item(sm, true); destinationItem = new MappedItem.Item(dm, false); mapping = new MappedItem(sourceItem, destinationItem, this); this.items.Add(mapping); } if (mapping != null) { if (complex) { SimpleMapper subMap = MapResolver.Resolve(sourceItem.Type, destinationItem.Type); mapping.IsComplex = true; subMaps.Add(mapping.Key, subMap); } } return(this); }
/// <summary> /// Sets destination object member value. /// </summary> /// <param name="mapping">The mapping<see cref="MappedItem.Item"/></param> /// <param name="destination">The destination<see cref="object"/></param> /// <param name="value">The value<see cref="object"/></param> private static void SetDestinationValue(MappedItem.Item mapping, object destination, object value) { //// check whether to set property value or invoke method if (mapping.Member == 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> /// <param name="mapping">The mapping<see cref="MappedItem.Item"/></param> /// <param name="source">The source<see cref="object"/></param> /// <returns>A value returned by member.</returns> private static object GetSourceValue(MappedItem.Item mapping, object source) { object value; //// check whether to get property value or invoke method if (mapping.Member == 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); }