private bool GetFieldMappingInfo(IAutomapRules map, PropertyInfo outPropertyInfo, PropertyInfo innPropertyInfo) { if (!CanMapField(outPropertyInfo, innPropertyInfo)) { return(false); } if (innPropertyInfo.PropertyType == outPropertyInfo.PropertyType) { TypeMapper.MapType(map, innPropertyInfo, outPropertyInfo); } else if (outPropertyInfo.PropertyType.Implements(typeof(IDictionary))) { DictionaryMapper.MapDictionary(map, innPropertyInfo, outPropertyInfo); } else if (outPropertyInfo.PropertyType.Implements(typeof(IEnumerable)) && !(outPropertyInfo.PropertyType == typeof(string))) { ListMapper.MapIEnumerable(map, innPropertyInfo, outPropertyInfo); } else { TypeMapper.MapType(map, innPropertyInfo, outPropertyInfo); } return(true); }
public void ExtractMappingDefinition(IAutomapRules map) { var innType = map.InType.GetProperties(BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public); var outType = map.OutType.GetProperties(BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.Public); GetMappingInfo(innType, outType, map); }
public void MapType(IAutomapRules map, PropertyInfo innPropertyInfo, PropertyInfo outPropertyInfo) { map.Fields.Add(new KeyValuePair <string, IMapRules>(innPropertyInfo.Name, new MapRules { InMemberName = innPropertyInfo.Name, OutMemberName = outPropertyInfo.Name, BasicType = MapHelper.GetConversionType(outPropertyInfo.PropertyType, innPropertyInfo.PropertyType), Parent = Parent })); }
public void MapType(IAutomapRules map, PropertyInfo innPropertyInfo, PropertyInfo outPropertyInfo) { map.Fields.Add(new KeyValuePair<string, IMapRules>(innPropertyInfo.Name, new MapRules { InMemberName = innPropertyInfo.Name, OutMemberName = outPropertyInfo.Name, BasicType = MapHelper.GetConversionType(outPropertyInfo.PropertyType, innPropertyInfo.PropertyType), Parent = Parent })); }
private void CreateMapingDefinition(Dictionary <string, string> simpleMap, IAutomapRules rule) { if (simpleMap.IsInstance()) { foreach (var mapItem in simpleMap) { rule.Fields.Add(new KeyValuePair <string, IMapRules>(mapItem.Key, new MapRules { InMemberName = mapItem.Key, Convertible = true, OutMemberName = mapItem.Value, BasicType = BasicTypes.Convertable })); } rule.BasicType = BasicTypes.ComplexType; rule.Convertible = false; } else { ExtractMappingDefinition(rule); } }
internal void MapDictionary(IAutomapRules map, PropertyInfo innPropertyInfo, PropertyInfo outPropertyInfo) { var innGenericType = innPropertyInfo.PropertyType.GetGenericArguments()[0]; var outGenericType = outPropertyInfo.PropertyType.GetGenericArguments()[0]; var innGenericType2 = innPropertyInfo.PropertyType.GetGenericArguments()[1]; var outGenericType2 = outPropertyInfo.PropertyType.GetGenericArguments()[1]; if (TypeExtensions.CanConvert(outGenericType, innGenericType) && TypeExtensions.CanConvert(outGenericType2, innGenericType2)) { map.Fields.Add(new KeyValuePair <string, IMapRules>(innPropertyInfo.Name, new MapRules { InMemberName = innPropertyInfo.Name, OutMemberName = outPropertyInfo.Name, Convertible = true, BasicType = BasicTypes.Dictionary, Parent = Parent })); } else if (TypeExtensions.CanConvert(outGenericType, innGenericType)) { map.Fields.Add(new KeyValuePair <string, IMapRules>(innPropertyInfo.Name, new MapRules { InMemberName = innPropertyInfo.Name, OutMemberName = outPropertyInfo.Name, Convertible = false, BasicType = BasicTypes.Dictionary, Parent = Parent })); if (!map.Parent.ContainsRule(new KeyValuePair <Type, Type>(innGenericType2, outGenericType2))) { map.Parent.AddMap(innGenericType2, outGenericType2); } } }
internal void MapIEnumerable(IAutomapRules map, PropertyInfo innPropertyInfo, PropertyInfo outPropertyInfo) { try { map.Fields.Add(new KeyValuePair <string, IMapRules>(innPropertyInfo.Name, new MapRules { InMemberName = innPropertyInfo.Name, OutMemberName = outPropertyInfo.Name, BasicType = BasicTypes.List, Parent = Parent })); var innGenericType = innPropertyInfo.PropertyType.GetGenericArguments()[0]; var outGenericType = outPropertyInfo.PropertyType.GetGenericArguments()[0]; if (!map.Parent.ContainsRule(new KeyValuePair <Type, Type>(innGenericType, outGenericType))) { map.Parent.AddMap(innGenericType, outGenericType); } } catch (Exception ex) { throw new InvalidCastException($"unabel to map {innPropertyInfo.Name}"); } }
public static object Convert(this IAutomapRules rule, object source) { return(Resolver.Activate <ITypeConverter>(rule.BasicType) .Initialize(rule) .Convert(source)); }
public static object Convert(this object source, IAutomapRules rule) { return Resolver.Activate<ITypeConverter>(rule.BasicType) .Initialize(rule) .Convert(source); }
private void GetMappingInfo(IEnumerable <PropertyInfo> innType, PropertyInfo[] outType, IAutomapRules map) { foreach (var innPropertyInfo in innType) { foreach (var outPropertyInfo in outType) { if (GetFieldMappingInfo(map, outPropertyInfo, innPropertyInfo)) { break; } } } }