private object MapObject(object source, object dest) { var destProperties = dest.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(x => x.CanWrite) .ToArray(); foreach (var destProp in destProperties) { var sourceProp = source.GetType() .GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(p => p.Name == destProp.Name) .FirstOrDefault(); if (sourceProp != null) { var sourceValue = sourceProp.GetMethod.Invoke(source, new object[0]); if (sourceValue == null) { throw new ArgumentException(ExceptionUtils.NullableSourceValueGetMethod); } if (ReflectionUtils.IsPrimitive(sourceValue.GetType())) { destProp.SetValue(dest, sourceProp.GetValue(source, null), null); continue; } if (ReflectionUtils.IsGenericCollection(sourceValue.GetType())) { if (ReflectionUtils.IsPrimitive(sourceValue.GetType().GetGenericArguments()[0])) { var destinationCollection = sourceValue; destProp.SetMethod.Invoke(dest, new[] { destinationCollection }); } else { var destColl = destProp.GetMethod.Invoke(dest, new object[0]); var destType = destColl.GetType().GetGenericArguments()[0]; foreach (var destP in (IEnumerable)sourceValue) { ((IList)destColl).Add(this.CreateMappedObject(destP, destType)); } } } else if (ReflectionUtils.IsNonGenericCollection(sourceValue.GetType())) { var destColl = (IList)Activator.CreateInstance(destProp.PropertyType, new object[] { ((object[])sourceValue).Length }); for (int i = 0; i < ((object[])sourceValue).Length; i++) { destColl[i] = this.CreateMappedObject(((object[])sourceValue)[i], destProp.PropertyType.GetElementType()); } destProp.SetValue(dest, destColl); } else { destProp.SetValue(dest, this.CreateMappedObject(sourceProp.GetValue(source), destProp.PropertyType)); } } } return(dest); }
private T DoMapping <T>(object source, T dest) { var properties = dest .GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.CanWrite); var srcProperties = source .GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { var srcProperty = srcProperties. Where(p => p.Name == property.Name) .FirstOrDefault(); if (srcProperty == null) { continue; } var sourceValue = srcProperty.GetMethod.Invoke(source, new object[0]); if (ReflectionUtils.IsPrimitive(sourceValue.GetType())) { property.SetValue(dest, srcProperty.GetValue(source)); continue; } if (ReflectionUtils.IsGenericCollection(sourceValue.GetType())) { if (ReflectionUtils.IsPrimitive(sourceValue.GetType().GetGenericArguments()[0])) { var destinationCollection = sourceValue; property.SetMethod.Invoke(dest, new[] { destinationCollection }); } else { var destColl = property.GetMethod.Invoke(dest, new object[0]); var destType = destColl.GetType().GetGenericArguments()[0]; foreach (var destP in (IEnumerable)sourceValue) { ((IList)destColl).Add(this.DoMapping(destP, destType)); } } } else if (ReflectionUtils.IsNonGenericCollection(sourceValue.GetType())) { var destColl = (IList)Activator.CreateInstance(property.PropertyType, new object[] { ((object[])sourceValue).Length }); for (int i = 0; i < ((object[])sourceValue).Length; i++) { destColl[i] = this.DoMapping(((object[])sourceValue)[i], property.PropertyType.GetElementType()); } property.SetValue(dest, destColl); } else { var propertyInstance = Activator.CreateInstance(srcProperty.GetValue(source).GetType()); property.SetValue(dest, this.DoMapping(srcProperty.GetValue(source), propertyInstance)); } } return(dest); }