Exemplo n.º 1
0
        private static void GetNonActiveNetClasses(ICollection <Schema> classes, Schema parentClass)
        {
            try
            {
                if ((classes != null) &&
                    (parentClass?.Type != null) &&
                    (parentClass.Columns != null) &&
                    (parentClass.Columns.Count == 0))
                {
                    var propertyArray = AnReflectionCache.GetPropertyInfoArray(parentClass.Type);

                    if (propertyArray?.Length > 0)
                    {
                        var properties = propertyArray.ToList();

                        if (properties.Count > 0)
                        {
                            foreach (var property in properties.Where(property => (property != null)))
                            {
                                var type = property.PropertyType;

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

                                if (AnSerialization.SerialSizeDictionary.ContainsKey(type) ||
                                    (type == typeof(string)) ||
                                    (type == typeof(byte[])))
                                {
                                    parentClass.Columns.Add(new Schema
                                    {
                                        FieldName     = property.Name,
                                        ClassName     = type.Name,
                                        ClassFullName = type.FullName,
                                        Type          = type
                                    });
                                }

                                else
                                {
                                    if (type.IsGenericType &&
                                        (type.GetGenericTypeDefinition() == typeof(List <>)))
                                    {
                                        var genericListItemType = GenericOfClassType(type.FullName);

                                        if (!string.IsNullOrEmpty(genericListItemType?.FullName))
                                        {
                                            parentClass.Columns.Add(new Schema
                                            {
                                                FieldName     = property.Name,
                                                ClassName     = type.Name,
                                                ClassFullName = type.FullName,
                                                Type          = type
                                            });

                                            if (!classes.Where(schema => schema != null).Any(schema => (schema.Type == genericListItemType) && string.Equals(schema.ClassFullName, genericListItemType?.FullName ?? "", StringComparison.CurrentCulture)))
                                            {
                                                var parentSchema = new Schema
                                                {
                                                    FieldName     = property.Name,
                                                    ClassName     = genericListItemType?.Name ?? "",
                                                    ClassFullName = genericListItemType?.FullName ?? "",
                                                    Type          = genericListItemType
                                                };

                                                classes.Add(parentSchema);

                                                GetNonActiveNetClasses(classes, parentSchema);
                                            }
                                        }
                                    }

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

                                        if (!string.IsNullOrEmpty(genericListItemType?.FullName))
                                        {
                                            parentClass.Columns.Add(new Schema
                                            {
                                                FieldName     = property.Name,
                                                ClassName     = type.Name,
                                                ClassFullName = type.FullName,
                                                Type          = type
                                            });

                                            if (!classes.Where(schema => schema != null).Any(schema => (schema.Type == genericListItemType) && string.Equals(schema.ClassFullName, genericListItemType?.FullName ?? "", StringComparison.CurrentCulture)))
                                            {
                                                var parentSchema = new Schema
                                                {
                                                    FieldName     = property.Name,
                                                    ClassName     = genericListItemType?.Name ?? "",
                                                    ClassFullName = genericListItemType?.FullName ?? "",
                                                    Type          = genericListItemType
                                                };

                                                classes.Add(parentSchema);

                                                GetNonActiveNetClasses(classes, parentSchema);
                                            }
                                        }
                                    }

                                    else if (!classes.Where(schema => schema != null).Any(schema => (schema.Type == type) && (schema.ClassFullName == type.FullName)))
                                    {
                                        parentClass.Columns.Add(new Schema
                                        {
                                            FieldName     = property.Name,
                                            ClassName     = type.Name,
                                            ClassFullName = type.FullName,
                                            Type          = type
                                        });

                                        var parentSchema = new Schema
                                        {
                                            FieldName     = property.Name,
                                            ClassName     = type.Name,
                                            ClassFullName = type.FullName,
                                            Type          = type
                                        };

                                        classes.Add(parentSchema);

                                        GetNonActiveNetClasses(classes, parentSchema);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                AnLog.Error(ex);
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public static Dictionary <string, Type> GetFieldNamesAndDataTypes <T>(T t)
        {
            var result = new Dictionary <string, Type>();

            try
            {
                if (t != null)
                {
                    var classType = AnTypes.GetType(t, null);

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

                        if (properties?.Length > 0)
                        {
                            foreach (var property in properties)
                            {
                                var propertyType = property.PropertyType;

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

                                Type type;

                                if (AnSerialization.SerialSizeDictionary.ContainsKey(propertyType) ||
                                    (propertyType == typeof(string)) ||
                                    (propertyType == typeof(byte[])))
                                {
                                    type = propertyType;
                                }

                                else
                                {
                                    if (propertyType.IsGenericType &&
                                        (propertyType.GetGenericTypeDefinition() == typeof(List <>)))
                                    {
                                        type = GenericOfClassType(propertyType.FullName);
                                    }

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

                                    else
                                    {
                                        type = GenericOfClassType(propertyType.FullName);
                                    }
                                }

                                if (type != null)
                                {
                                    result.Add(property.Name, type);
                                }
                            }
                        }
                    }
                }
            }

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

            return(result);
        }
Exemplo n.º 4
0
        public static Dictionary <string, object> ToDictionary <T>(T t)
        {
            var result = new Dictionary <string, object>();

            try
            {
                if (t != null)
                {
                    var classType = AnTypes.GetType(t, null);

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

                        if (properties?.Length > 0)
                        {
                            foreach (var property in properties)
                            {
                                var propertyType = property.PropertyType;

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

                                if (!string.IsNullOrEmpty(propertyType.FullName))
                                {
                                    object value = null;

                                    if (AnSerialization.SerialSizeDictionary.ContainsKey(propertyType) ||
                                        (propertyType == typeof(string)) ||
                                        (propertyType == typeof(byte[])))
                                    {
                                        value = property.GetValue(t, null);
                                    }

                                    else
                                    {
                                        if (propertyType.IsGenericType &&
                                            (propertyType.GetGenericTypeDefinition() == typeof(List <>)))
                                        {
                                            var toDictionaryList = AnReflectionCache.GetToDictionaryListMethod(typeof(AnCloneUtility));

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

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

                                                    value = toDictionaryList.Invoke(null, new[] { property.GetValue(t, null) });
                                                }
                                            }
                                        }

                                        else if ((propertyType.BaseType != null) &&
                                                 propertyType.BaseType.IsGenericType &&
                                                 (propertyType.BaseType.GetGenericTypeDefinition() == typeof(List <>)))
                                        {
                                            var toDictionaryList = AnReflectionCache.GetToDictionaryListMethod(typeof(AnCloneUtility));

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

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

                                                    value = toDictionaryList.Invoke(null, new[] { property.GetValue(t, null) });
                                                }
                                            }
                                        }

                                        else
                                        {
                                            var toDictionary = AnReflectionCache.GetToDictionaryMethod(typeof(AnCloneUtility));

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

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

                                                    value = toDictionary.Invoke(null, new[] { property.GetValue(t, null) });
                                                }
                                            }
                                        }
                                    }

                                    result.Add(property.Name.ToLower(), value);
                                }
                            }
                        }
                    }
                }
            }

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

            return(result);
        }