public static T FasterMap <T>(this object source, T destination) { if (source == null) { return(default(T)); } if (source is IEnumerable) { var destinationMain = destination as IList; foreach (var item in source as IEnumerable) { var srcTypeL = GetTypeCustom(item); var srcPropertiesL = GetListPropertyInfo(srcTypeL); var destinationTypeL = GetTypeGeneric(destination); var destPropertiesL = GetListPropertyInfo(destinationTypeL); var instance = Activator.CreateInstance(destinationTypeL); foreach (var property in srcPropertiesL) { GetTypeOfVariable(property, out var isSystemType, out var isICollection); if (isSystemType && !isICollection) { var destPropertName = Util.CalculateSimilarity(property.Name, destPropertiesL) .FirstOrDefault()?.DestPropertName; var bestMatch = destPropertiesL.Where(r => destPropertName == r.Name); bestMatch.FirstOrDefault()?.SetValue(instance, property.GetValue(item, null)); } else if (isSystemType) { var props = TypeDescriptor.GetProperties(destinationTypeL); var destPropertName = Util.CalculateSimilarity(property.Name, destPropertiesL) .FirstOrDefault()?.DestPropertName ?? throw new InvalidOperationException(); var selectedProperty = destPropertiesL.FirstOrDefault(r => r.Name == destPropertName); if (selectedProperty == null) { continue; } var instance1 = Activator.CreateInstance(selectedProperty.PropertyType); var value = property.GetValue(item, null); var mappedData = value.FasterMap(instance1 as IList); props[destPropertName].SetValue(instance, mappedData); } } destinationMain?.Add(instance); } return((T)destinationMain); } { var srcType = GetTypeCustom(source); var srcProperties = GetListPropertyInfo(srcType); // store this collection for optimum performance var props = TypeDescriptor.GetProperties( typeof(T)); var destinationType = GetTypeCustom(destination); var destProperties = GetListPropertyInfo(destinationType); foreach (var property in srcProperties) { GetTypeOfVariable(property, out var isSystemType, out var isICollection); if (isSystemType && !isICollection) { var destPropertName = Util.CalculateSimilarity(property.Name, destProperties).FirstOrDefault() ?.DestPropertName; var bestMatch = destProperties.Where(r => destPropertName == r.Name); bestMatch.FirstOrDefault()?.SetValue(destination, property.GetValue(source, null)); } else if (isSystemType) { var destPropertName = Util.CalculateSimilarity(property.Name, destProperties) .FirstOrDefault()?.DestPropertName ?? throw new InvalidOperationException(); var selectedProperty = destProperties.FirstOrDefault(r => r.Name == destPropertName); if (selectedProperty != null) { var instance = Activator.CreateInstance(selectedProperty.PropertyType); var value = property.GetValue(source, null); var mappedData = value.FasterMap(instance as IList); props[destPropertName].SetValue(destination, mappedData); } } else { if (property == null) { continue; } var destPropertName = Util.CalculateSimilarity(property.Name, destProperties) .FirstOrDefault()?.DestPropertName ?? throw new InvalidOperationException(); var selectedProperty = destProperties.FirstOrDefault(r => r.Name == destPropertName); if (selectedProperty == null) { continue; } var instance = Activator.CreateInstance(selectedProperty.PropertyType); var mappedData = property.GetValue(source, null).FasterMap(instance); props[destPropertName].SetValue(destination, mappedData); } } return(destination); } }
public static T ComplexMap <T>(this object source, T destination, Func <T, string[]> exceptPred) where T : new() { if (source == null) { return(default(T)); } var excludePropertie = exceptPred.Invoke(new T()); var srcType = GetTypeCustom(source); var srcProperties = GetListPropertyInfo(srcType); // store this collection for optimum performance var props = TypeDescriptor.GetProperties(destination.GetType()); var destinationType = GetTypeCustom(destination); var destProperties = GetListPropertyInfo(destinationType); foreach (var property in srcProperties) { if (excludePropertie.Contains(property.Name)) { continue; } GetTypeOfVariable(property, out var isSystemType, out var isICollection); if (isSystemType && !isICollection) { var destPropertName = Util.CalculateSimilarity(property.Name, destProperties) .FirstOrDefault()?.DestPropertName ?? throw new InvalidOperationException(); props[destPropertName] .SetValue(destination, property.GetValue(source, null)); } else if (isSystemType) { var destPropertName = Util.CalculateSimilarity(property.Name, destProperties) .FirstOrDefault()?.DestPropertName ?? throw new InvalidOperationException(); var selectedProperty = destProperties.FirstOrDefault(r => r.Name == destPropertName); if (selectedProperty != null) { var instance = Activator.CreateInstance(selectedProperty.PropertyType); var value = property.GetValue(source, null); var mappedData = value.FasterMap(instance as IList); props[destPropertName].SetValue(destination, mappedData); } } else { if (property != null) { var destPropertName = Util.CalculateSimilarity(property.Name, destProperties) .FirstOrDefault()?.DestPropertName ?? throw new InvalidOperationException(); var selectedProperty = destProperties.FirstOrDefault(r => r.Name == destPropertName); if (selectedProperty != null) { var instance = Activator.CreateInstance(selectedProperty.PropertyType); var data = property.GetValue(source, null).ComplexMap(instance); props[destPropertName].SetValue(destination, data); } } } } return(destination); }