/// <summary> /// After determining the generic type, adds a configuration of that type to the mapping actions /// </summary> private void AddMappingActions <T>(KeyValuePair <System.Reflection.PropertyInfo, PropertyMapping> item) { var configuration = new PropertyMappingConfiguration <T>(item.Key); configuration.UseAlias(item.Value.ActiveAlias); _mappingActions.Add(item.Key.Name, configuration); }
public ICopyStrategy GetStrategy(ClassMappingConfiguration classConfig, PropertyMappingConfiguration propConfig) { if (classConfig.ConvertPrimitives) { if (propConfig.RejectNullReferences) { return(new ConvertCopyStrategy(new List <IPropertyLevelRule>() { new RejectNullReferencesPropertyLevelRule() })); } else { return(new ConvertCopyStrategy(null)); } } else { if (propConfig.RejectNullReferences) { return(new DefaultCopyStrategy(new List <IPropertyLevelRule>() { new RejectNullReferencesPropertyLevelRule() })); } else { return(new DefaultCopyStrategy(null)); } } }
public void Run <TOut>(PropertyMappingConfiguration toPropConfig, object tFrom, TOut tTo, PropertyInfo toProp, PropertyInfo fromProp) { if (toPropConfig.RejectNullReferences && (fromProp.GetValue(tFrom) is null)) { throw new NullReferenceException(@"A null property reference was passed into the mapper. If this is not desired behavior please remove the property level attribute entitled [RejectNullReferences] from [" + tTo.GetType().Name + "].[" + toProp.Name + "]"); } }
protected PropertyMappingConfiguration GetPropertyMappingConfiguration(PropertyInfo p) { var model = new PropertyMappingConfiguration(); var attrs = p.GetCustomAttributes().ToList(); //model.Bypass = attrs.Any(a => a is BypassAttribute); model.RejectNullReferences = attrs.Any(a => a is RejectNullReferencesAttribute); return(model); }
public static void ColumnName_is_set_on_internal_configuration() { var primitivePropertyConfiguration = new PrimitivePropertyConfiguration(); primitivePropertyConfiguration.ColumnName = "A"; Assert.Equal("A", primitivePropertyConfiguration.ColumnName); var propertyMappingConfiguration = new PropertyMappingConfiguration(primitivePropertyConfiguration); propertyMappingConfiguration.HasColumnName("B"); Assert.Equal("B", primitivePropertyConfiguration.ColumnName); }
public static void ColumnName_is_set_on_internal_configuration() { var primitivePropertyConfiguration = new PrimitivePropertyConfiguration(); primitivePropertyConfiguration.ColumnName = "A"; Assert.Equal("A", primitivePropertyConfiguration.ColumnName); var propertyMappingConfiguration = new PropertyMappingConfiguration(primitivePropertyConfiguration); propertyMappingConfiguration.HasColumnName("B"); Assert.Equal("B", primitivePropertyConfiguration.ColumnName); }
public void Copy <TOut>(object tFrom, TOut tTo, PropertyInfo toProp, PropertyInfo fromProp, PropertyMappingConfiguration toPropConfig) { if (!ShouldCopy(tFrom, tTo, toProp, fromProp, toPropConfig)) { return; } if (_rules != null && _rules.Any()) { _rules.ToList().ForEach(a => a.Run(toPropConfig, tFrom, tTo, toProp, fromProp)); } var tFromType = tFrom.GetType(); var tToType = tTo.GetType(); var fromVal = fromProp.GetValue(tFrom); //no casting needed if (toProp.PropertyType == fromProp.PropertyType) { toProp.SetValue(tTo, fromVal); return; } //if destination is string then job is easy if (toProp.PropertyType == typeof(string)) { toProp.SetValue(tTo, fromVal?.ToString()); return; } //destination is some number type if (new List <Type>() { typeof(DateTime), typeof(TimeSpan), typeof(DateTime?), typeof(TimeSpan?) }.Contains(toProp.PropertyType)) { //destination type is date or timespan value ConvertToDateOrTimeValue(tFrom, tTo, toProp, fromProp); } else if (toProp.PropertyType.IsEnum) { ConvertToEnum(tFrom, tTo, toProp, fromProp); } else if (fromProp.PropertyType.IsEnum && toProp.PropertyType == typeof(int)) { toProp.SetValue(tTo, (int)fromProp.GetValue(tFrom)); } else if (fromProp.PropertyType.IsEnum && toProp.PropertyType != typeof(int)) { throw new InvalidCastException($"Cannot convert from {tFrom.GetType().Name}.{fromProp.Name} to " + $"{tTo.GetType().Name}.{toProp.Name}. {tTo.GetType().Name}.{toProp.Name} must be an integer"); } else { ConvertToNumber(tFrom, tTo, toProp, fromProp); } }
public void Copy <TOut>(object tFrom, TOut tTo, PropertyInfo toProp, PropertyInfo fromProp, PropertyMappingConfiguration toPropConfig) { //do nothing }
public void Copy <TOut>(object tFrom, TOut tTo, PropertyInfo toProp, PropertyInfo fromProp, PropertyMappingConfiguration toPropConfig) { if (!ShouldCopy(tFrom, tTo, toProp, fromProp, toPropConfig)) { return; } if (_rules != null && _rules.Any()) { _rules.ToList().ForEach(a => a.Run(toPropConfig, tFrom, tTo, toProp, fromProp)); } if (toProp.PropertyType != fromProp.PropertyType) { throw new InvalidCastException($"{tFrom.GetType()}.{toProp.Name} is not the same type as {tTo.GetType()}.{fromProp.Name}"); } var fromVal = fromProp.GetValue(tFrom); toProp.SetValue(tTo, fromVal); }
protected bool ShouldCopy <TOut>(object tFrom, TOut tTo, PropertyInfo toProp, PropertyInfo fromProp, PropertyMappingConfiguration toPropConfig) { if (toProp.PropertyType.IsEnum && Constants.TypeWhiteList.Contains(fromProp.PropertyType)) { return(true); } else if (fromProp.PropertyType.IsEnum && Constants.TypeWhiteList.Contains(toProp.PropertyType)) { return(true); } else if (fromProp.PropertyType.IsEnum && toProp.PropertyType.IsEnum) { return(true); } if (!Constants.TypeWhiteList.Contains(toProp.PropertyType) || !Constants.TypeWhiteList.Contains(fromProp.PropertyType)) { return(false); } else { return(true); } }