Example #1
0
        public static T FromDictionary <T>(IDictionary <string, object> dictionary) where T : new()
        {
            var result = new T();

            try
            {
                if ((dictionary != null) &&
                    (dictionary.Count > 0))
                {
                    dictionary = AnTypes.LowerCaseKeys(dictionary);

                    var classType = AnTypes.GetType(result, null);

                    if (classType != null)
                    {
                        var properties = AnReflectionCache.GetPropertyInfoArray(classType);

                        if (properties?.Length > 0)
                        {
                            foreach (var property in properties.Where(property => dictionary.ContainsKey(property.Name.ToLower())))
                            {
                                var propertyType = property.PropertyType;

                                if (property.PropertyType.IsEnum)
                                {
                                    propertyType = AnReflectionCache.GetUnderlyingType(propertyType);
                                }

                                var key = property.Name.ToLower();

                                object value = null;

                                if (AnSerialization.SerialSizeDictionary.ContainsKey(propertyType))
                                {
                                    value = AnSafeConvert.ChangeType(dictionary[key], propertyType);
                                }

                                else if (propertyType == typeof(string))
                                {
                                    value = AnSafeConvert.ToString(dictionary[key]);
                                }

                                else if (propertyType == typeof(byte[]))
                                {
                                    value = dictionary[key] as byte[];
                                }

                                else
                                {
                                    if (dictionary[key] is Dictionary <string, object> subDictionary)
                                    {
                                        var fromDictionary = AnReflectionCache.GetFromDictionaryMethod(typeof(AnCloneUtility));

                                        if (fromDictionary != null)
                                        {
                                            var genericListItemType = GenericOfClassType(propertyType.FullName);

                                            if (genericListItemType != null)
                                            {
                                                fromDictionary = fromDictionary.MakeGenericMethod(genericListItemType);

                                                value = fromDictionary.Invoke(null, new object[] { subDictionary });
                                            }
                                        }
                                    }

                                    else
                                    {
                                        if ((dictionary[key] is List <Dictionary <string, object> > dictionaryList) &&
                                            (dictionaryList.Count > 0))
                                        {
                                            if (propertyType.IsGenericType &&
                                                (propertyType.GetGenericTypeDefinition() == typeof(List <>)))
                                            {
                                                var genericListItemType = GenericOfClassType(propertyType.FullName);

                                                if (genericListItemType != null)
                                                {
                                                    var fromDictionaryList = AnReflectionCache.GetFromDictionaryListMethod(typeof(AnCloneUtility));

                                                    if (fromDictionaryList != null)
                                                    {
                                                        fromDictionaryList = fromDictionaryList.MakeGenericMethod(genericListItemType);

                                                        value = fromDictionaryList.Invoke(null, new object[] { dictionaryList });
                                                    }
                                                }
                                            }

                                            else if ((propertyType.BaseType != null) &&
                                                     propertyType.BaseType.IsGenericType &&
                                                     (propertyType.BaseType.GetGenericTypeDefinition() == typeof(List <>)))
                                            {
                                                var genericListItemType = GenericOfClassType(propertyType.BaseType.FullName);

                                                if (genericListItemType != null)
                                                {
                                                    var fromDictionaryList = AnReflectionCache.GetFromDictionaryListMethod(typeof(AnCloneUtility));

                                                    if (fromDictionaryList != null)
                                                    {
                                                        fromDictionaryList = fromDictionaryList.MakeGenericMethod(genericListItemType);

                                                        var listObject = fromDictionaryList.Invoke(null, new object[] { dictionaryList });

                                                        if (listObject != null)
                                                        {
                                                            value = AnTypes.CreateItemInstance(propertyType.FullName, propertyType, new[] { listObject });
                                                        }
                                                    }
                                                }
                                            }

                                            else
                                            {
                                                var fromDictionary = AnReflectionCache.GetFromDictionaryMethod(typeof(AnCloneUtility));

                                                if (fromDictionary != null)
                                                {
                                                    var genericListItemType = GenericOfClassType(propertyType.FullName);

                                                    if (genericListItemType != null)
                                                    {
                                                        fromDictionary = fromDictionary.MakeGenericMethod(genericListItemType);

                                                        value = fromDictionary.Invoke(null, new object[] { dictionaryList[0] });
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }

                                property.SetValue(result, value);
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                AnLog.Error(ex);
            }

            return(result);
        }