public static T MapTo <T>(this object obj, IEnumerable <Converter> converters) where T : new() { ConverterResolver converterResolver = new StandartConverterResolver(true); foreach (var converter in converters) { converterResolver.Add(converter); } return(MapTo <T>(obj, converterResolver)); }
public static T MapToWithoutAttribute <T>(this object obj, params Converter[] converters) where T : new() { if (converters.Length == 0) { return(MapToWithoutAttribute <T>(obj, new DefaultConverterResolver())); } ConverterResolver converterResolver = new StandartConverterResolver(true); foreach (var converter in converters) { converterResolver.Add(converter); } return(MapToWithoutAttribute <T>(obj, converterResolver)); }
/// <summary> /// Maps to any generic type using DefaultConverterResolver if not converter is passed. /// If some converters are passed, then StandartConverterResolver is used, also the Default Converter is use if /// not default converter was specified. /// </summary> /// <returns>A completely new object is returned where its properties have the value of the properties on the caller object that match</returns> /// <param name="obj">The caller object. Its properties are read and then copied to the new object result</param> /// <param name="converters">Each converter should has the ability to convert from one data type to another. /// If not converter is passed, then the DefaultConverterResolver will be used</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T MapTo <T>(this object obj, params Converter[] converters) where T : new() { if (converters.Length == 0) { return(MapTo <T>(obj, new DefaultConverterResolver())); } ConverterResolver converterResolver = new StandartConverterResolver(true); foreach (var converter in converters) { converterResolver.Add(converter); } return(MapTo <T>(obj, converterResolver)); #region old //T result = new T(); //foreach (var propertyInfo in result.GetType().GetProperties()) //{ // PropertyNameAttribute t = (PropertyNameAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(PropertyNameAttribute)); // if (t != null) // { // PropertyInfo sourceProperty; // if (t.Type == null) // { // sourceProperty = obj.GetType().GetProperty(t.Name); // } // else // { // sourceProperty = obj.GetType().GetProperty(t.Name, t.Type); // } // if (sourceProperty == null) // { // throw new InvalidMapException("There is not a property on the DB object which matches with " + t.Name + " from the domain object"); // } // try // { // propertyInfo.SetValue(result, sourceProperty.GetValue(obj, null), null); // } // catch (Exception e) // { // throw new InvalidMapException(string.Format("The types of the properties {0} on {1} and {2} on {3} do not match. " + // "See Inner Exception for more details.", propertyInfo.Name, // result.GetType().Name, sourceProperty.Name, obj.GetType().Name), e); // } // } //} //return result; #endregion }