Пример #1
0
        public static bool Exists <T>(T value, int id)
        {
            try
            {
                if (value != null)
                {
                    var classType = AnTypes.GetType(value, null);

                    if (classType != null)
                    {
                        foreach (var property in classType.GetProperties())
                        {
                            var propertyType = property.PropertyType;

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

                            var key = property.Name.ToLower();

                            if ((Type.GetTypeCode(propertyType) == TypeCode.Int32) && (key == "id"))
                            {
                                var localId = (int)property.GetValue(value, null);

                                if (localId == id)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }

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

            return(false);
        }
Пример #2
0
        private static XElement GenerateClassElement(Type parentClassType)
        {
            var result = new XElement("Fields");

            try
            {
                if (parentClassType != null)
                {
                    var properties = AnReflectionCache.GetPropertyInfoList(parentClassType);

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

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

                            var className = type.FullName;

                            if (!string.IsNullOrEmpty(className))
                            {
                                result.Add(new XElement(property.Property.Name, new XAttribute("Type", TrimClassName(className))));
                            }
                        }
                    }
                }
            }

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

            return(result);
        }
Пример #3
0
        public static bool ExistsInList <T>(T list, int id)
        {
            try
            {
                if (list != null)
                {
                    var classType = typeof(T);
                    var className = classType.FullName;

                    var exists = AnReflectionCache.GetExistsMethod(typeof(AnDataSetUtility));

                    if (exists != null)
                    {
                        var genericListItemType = GenericOfClassType(className);

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

                            if (classType.GetInterfaces().Contains(typeof(IEnumerable)))
                            {
                                if (((IEnumerable)list).Cast <object>().Any(item => (bool)exists.Invoke(null, new[] { item, id })))
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }

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

            return(false);
        }
Пример #4
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);
            }
        }
Пример #5
0
        private static void GetClasses(Type parentClassType, IDictionary <string, Type> classes)
        {
            try
            {
                if ((parentClassType != null) &&
                    (classes != null))
                {
                    var properties = AnReflectionCache.GetPropertyInfoList(parentClassType);

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

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

                            if (!(AnSerialization.SerialSizeDictionary.ContainsKey(type) ||
                                  (type == typeof(string)) ||
                                  (type == typeof(byte[]))))
                            {
                                if (!string.IsNullOrEmpty(type.BaseType?.Name) &&
                                    (type.BaseType?.Name ?? "").ToLower().Contains("anbaseobjectlist"))
                                {
                                    if (!string.IsNullOrEmpty(type.BaseType?.FullName))
                                    {
                                        var subClassName = GenericOfClassName(type.BaseType?.FullName);

                                        if (!string.IsNullOrEmpty(subClassName))
                                        {
                                            if (!classes.ContainsKey(subClassName))
                                            {
                                                var subType = AnTypes.GetType(subClassName, null);

                                                if (subType != null)
                                                {
                                                    classes.Add(subClassName, subType);

                                                    GetClasses(subType, classes);
                                                }
                                            }
                                        }
                                    }
                                }

                                else
                                {
                                    if (!string.IsNullOrEmpty(type.FullName) &&
                                        !classes.ContainsKey(type.FullName))
                                    {
                                        classes.Add(type.FullName, type);

                                        GetClasses(type, classes);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                AnLog.Error(ex);
            }
        }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
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);
        }