示例#1
0
        public static T MapTo <T>(this object obj, ConverterResolver converterResolver) where T : new()
        {
            return(MapAtrributes <T>(obj, converterResolver, true));

//            if (converterResolver == null)
//            {
//                throw new ArgumentNullException("converterResolver");
//            }
//
//            T result = new T();
//
//            foreach (var propertyInfo in result.GetType().GetProperties())
//            {
//                MapPropertyNameAttribute t = (MapPropertyNameAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(MapPropertyNameAttribute));
//
//                if (t != null)
//                {
//                    PropertyInfo sourceProperty = t.Type == null ? obj.GetType().GetProperty(t.Name) : obj.GetType().GetProperty(t.Name, t.Type);
//
//                    if (sourceProperty == null)
//                    {
//                        throw new InvalidMapException(string.Format("There is not a property on {0} object which matches with {1} from the target object", obj, t.Name));
//                    }
//
//                    try
//                    {
//                      Type inType = sourceProperty.PropertyType;
//                        Type outType = propertyInfo.PropertyType;
//
//                        object valueToConvert = sourceProperty.GetValue(obj, null);
//
//                        if (valueToConvert != null)
//                        {
//                            MethodInfo genericMethod = converterResolver.GetType().GetMethod("Convert").MakeGenericMethod(inType, outType);
//                            object valueToSet = genericMethod.Invoke(converterResolver, new[] {valueToConvert});
//
//                            propertyInfo.SetValue(result, valueToSet, null);
//                        }
//                        else
//                        {
//                            Expression<Func<object>> e = Expression.Lambda<Func<object>>(Expression.Convert(Expression.Default(outType), outType));
//
//                            object defaultValue = e.Compile()(); //outType.IsValueType ? Activator.CreateInstance(outType) : null;
//                            propertyInfo.SetValue(result, defaultValue, 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;
        }
示例#2
0
        private static T MapAtrributes <T>(object obj, ConverterResolver converterResolver, bool useAttribute) where T : new()
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            if (converterResolver == null)
            {
                throw new ArgumentNullException("converterResolver");
            }

            T result = new T();

            foreach (var propertyInfo in result.GetType().GetProperties())
            {
                PropertyInfo sourceProperty = null;

                if (useAttribute)
                {
                    MapPropertyNameAttribute t = (MapPropertyNameAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(MapPropertyNameAttribute));

                    if (t == null)
                    {
                        continue;
                    }

                    sourceProperty = t.Type == null?obj.GetType().GetProperty(t.Name) : obj.GetType().GetProperty(t.Name, t.Type);

                    if (sourceProperty == null)
                    {
                        throw new InvalidMapException(string.Format("There is not a property on {0} object which matches with {1} from the target object", obj, t.Name));
                    }
                }
                else
                {
                    sourceProperty = obj.GetType().GetProperty(propertyInfo.Name);

                    if (sourceProperty == null)
                    {
                        continue;
                    }
                }

                try
                {
                    Type inType  = sourceProperty.PropertyType;
                    Type outType = propertyInfo.PropertyType;

                    object valueToConvert = sourceProperty.GetValue(obj, null);

                    if (valueToConvert != null)
                    {
                        MethodInfo genericMethod = converterResolver.GetType().GetMethod("Convert").MakeGenericMethod(inType, outType);
                        object     valueToSet    = genericMethod.Invoke(converterResolver, new[] { valueToConvert });

                        propertyInfo.SetValue(result, valueToSet, null);
                    }
                    else
                    {
                        Expression <Func <object> > e = Expression.Lambda <Func <object> >(Expression.Convert(Expression.Default(outType), outType));

                        object defaultValue = e.Compile()(); //outType.IsValueType ? Activator.CreateInstance(outType) : null;
                        propertyInfo.SetValue(result, defaultValue, 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);
        }
示例#3
0
 public static T MapToWithoutAttribute <T>(this object obj, ConverterResolver converterResolver) where T : new()
 {
     return(MapAtrributes <T>(obj, converterResolver, false));
 }