public static void Map <TTo>(IDictionary <string, object> dictionary, TTo instance)
            {
                var attr = BindingFlags.Public | BindingFlags.Instance;

                foreach (var prop in instance.GetType().GetProperties(attr))
                {
                    if (prop.CanWrite && dictionary.ContainsKey(prop.Name))
                    {
                        var propType  = prop.PropertyType;
                        var dataValue = dictionary[prop.Name];
                        if (OnTypes.IsSimple(propType))
                        {
                            var v = Convert.ChangeType(dataValue, propType);
                            prop.SetValue(instance, v);
                        }
                        else
                        {
                            //TODO:when types match or are convertable, just assign
                            if (false)
                            {
                            }
                            //when corresponding prop name in the dictionary of complex type is a IDictionary<string, object>
                            if (dataValue is IDictionary <string, object> )
                            {
                                var v = TurnDictionaryIntoObject((IDictionary <string, object>)dataValue, propType);
                                prop.SetValue(instance, v);
                            }
                        }
                    }
                }
            }
 public static void Map <TFrom, TTo>(TFrom from, TTo to)
 {
     if (OnTypes.IsAssignable(typeof(TFrom), typeof(IDictionary <string, object>)))
     {
         Map((IDictionary <string, object>)from, to);
     }
     else
     {
         var data = OnMappings.TurnObjectIntoDictionary(from);
         OnMappings.Map(data, to);
     }
 }
Exemplo n.º 3
0
            public static IDictionary <string, object> TurnObjectIntoDictionary(object data)
            {
                var attr = BindingFlags.Public | BindingFlags.Instance;
                var dict = new Dictionary <string, object>();

                foreach (var prop in data.GetType().GetProperties(attr))
                {
                    if (prop.CanRead)
                    {
                        if (OnTypes.IsClass(prop.GetValue(data).GetType()) && !OnTypes.IsSimple(prop.GetValue(data).GetType()))
                        {
                            dict.Add(prop.Name, TurnObjectIntoDictionary(prop.GetValue(data)));
                        }
                        else
                        {
                            dict.Add(prop.Name, prop.GetValue(data, null));
                        }
                    }
                }
                return(dict);
            }
 public static IEnumerable <Type> GetInterfaces(this Type type)
 {
     return(OnTypes.GetInterfaces(type));
 }
 public static IEnumerable <Type> GetGenericArguments(this Type type)
 {
     return(OnTypes.GetGenericArguments(type));
 }
 public static bool IsAssignableFrom(this Type abstraction, Type concretion)
 {
     return(OnTypes.IsAssignable(concretion, abstraction));
 }
 public static Assembly Assembly(this Type type)
 {
     return(OnTypes.GetAssembly(type));
 }
 public static bool IsPrimitive(this Type type)
 {
     return(OnTypes.IsPrimitive(type));
 }
 public static bool IsGenericType(this Type type)
 {
     return(OnTypes.IsGenericType(type));
 }
 public static bool IsClass(this Type type)
 {
     return(OnTypes.IsClass(type));
 }
 public static bool IsInterface(this Type type)
 {
     return(OnTypes.IsInterface(type));
 }
 public static bool IsAbstract(this Type type)
 {
     return(OnTypes.IsAbstract(type));
 }