public static T AutoMap <T>(this T Destination, object Source, bool trimStrings = true, bool autoCapitalizeStrings = false) where T : class { foreach (PropertyInfo SourceProp in Source.GetType().GetProperties()) { var DestinationProp = Destination.GetType().GetProperty(SourceProp.Name); if (DestinationProp != null && DestinationProp.SetMethod.IsPublic && DestinationProp.CanWrite && SourceProp.GetMethod.IsPublic && SourceProp.CanRead && DestinationProp.GetType() == SourceProp.GetType() && DestinationProp.GetType().IsAssignableFrom(SourceProp.GetType()) ) { var value = SourceProp.GetValue(Source); if (value != null && SourceProp.PropertyType == typeof(string)) { if (trimStrings) { value = value.ToString().Trim(); } if (autoCapitalizeStrings) { value = value.ToString().ToUpper(); } } DestinationProp.SetValue(Destination, value); } } return(Destination); }
public static TDestination CopyProperties <TDestination>(object LinqSource) { Type SourceType = LinqSource.GetType(); Type DestinationType = typeof(TDestination); System.Reflection.ConstructorInfo ci = DestinationType.GetConstructor(new Type[0]); TDestination rez = (TDestination)ci.Invoke(new object[0]); object ComplexSourceProp = null; Type ComplexSourcePropType = null; System.Reflection.PropertyInfo[] SourceProps = SourceType.GetProperties(); foreach (System.Reflection.PropertyInfo SourceProp in SourceProps) { if (SourceProp.PropertyType.IsClass) { ComplexSourceProp = SourceProp.GetValue(LinqSource, null); ComplexSourcePropType = ComplexSourceProp.GetType(); break; } } System.Reflection.FieldInfo[] DestFields = typeof(TDestination).GetFields(); foreach (System.Reflection.FieldInfo DestField in DestFields) { bool DataIgnore = false; object[] attr = DestField.GetCustomAttributes(typeof(DataIgnoreAttrib), true); if (attr.Length > 0) { DataIgnore = true; } string DestPropName = DestField.Name; string UpDestPropName = DestPropName.Substring(0, 1).ToUpper() + DestPropName.Substring(1); attr = DestField.GetCustomAttributes(typeof(DataNameAttrib), true); if (attr.Length > 0) { DestPropName = ((DataNameAttrib)attr[0]).DataName; UpDestPropName = DestPropName.Substring(0, 1).ToUpper() + DestPropName.Substring(1); } System.Reflection.PropertyInfo SourceProp = SourceType.GetProperty(DestPropName); if (SourceProp == null) { SourceProp = SourceType.GetProperty(UpDestPropName); } if (SourceProp == null && DataIgnore) { continue; } if (SourceProp == null) { SourceProp = ComplexSourcePropType.GetProperty(DestPropName); if (SourceProp == null) { SourceProp = ComplexSourcePropType.GetProperty(UpDestPropName); } //************************************************************************************************************************************************************************** //if (SourceProp == null) continue;//************************************************************************************************************************************************************************** //if (SourceProp.PropertyType.IsClass) continue; //************************************************************************************************************************************************************************** object sv = SourceProp.GetValue(ComplexSourceProp, null); DestField.SetValue(rez, sv); } else { DestField.SetValue(rez, SourceProp.GetValue(LinqSource, null)); } } return(rez); }