コード例 #1
0
            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);
                            }
                        }
                    }
                }
            }
コード例 #2
0
 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);
     }
 }
コード例 #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);
            }
コード例 #4
0
 public static IEnumerable <Type> GetInterfaces(this Type type)
 {
     return(OnTypes.GetInterfaces(type));
 }
コード例 #5
0
 public static IEnumerable <Type> GetGenericArguments(this Type type)
 {
     return(OnTypes.GetGenericArguments(type));
 }
コード例 #6
0
 public static bool IsAssignableFrom(this Type abstraction, Type concretion)
 {
     return(OnTypes.IsAssignable(concretion, abstraction));
 }
コード例 #7
0
 public static Assembly Assembly(this Type type)
 {
     return(OnTypes.GetAssembly(type));
 }
コード例 #8
0
 public static bool IsPrimitive(this Type type)
 {
     return(OnTypes.IsPrimitive(type));
 }
コード例 #9
0
 public static bool IsGenericType(this Type type)
 {
     return(OnTypes.IsGenericType(type));
 }
コード例 #10
0
 public static bool IsClass(this Type type)
 {
     return(OnTypes.IsClass(type));
 }
コード例 #11
0
 public static bool IsInterface(this Type type)
 {
     return(OnTypes.IsInterface(type));
 }
コード例 #12
0
 public static bool IsAbstract(this Type type)
 {
     return(OnTypes.IsAbstract(type));
 }