Exemple #1
0
        public CacheList(string aFileName, string aKeyProperty, System.Type aCacheType)
            : base()
        {
            System.Console.WriteLine("Initializing CACHE " + aFileName);
            keyProperty = aKeyProperty;
            if (KeyProperty == "")
            {
                throw new Exception("Key property for cache list can't be empty");
            }
            cacheType = aCacheType;

            if (CacheType == null)
            {
                throw new Exception("Cache type for cache list can't be null");
            }
            PropertyInfo info = CacheType.GetProperty(KeyProperty);

            keyinfo = info;
            if (info == null)
            {
                throw new Exception("Property " + KeyProperty + " not found in " + aCacheType + " class!");
            }
            fileName = aFileName;
            if (FileName == "")
            {
                throw new Exception("Filename for cache list can't be empty");
            }

            PropertyInfo[] infos = aCacheType.GetProperties();
            foreach (PropertyInfo i in infos)
            {
                if ((i.CanRead == true) && (i.CanWrite == true))
                {
                    object[] attrs = i.GetCustomAttributes(typeof(CachedPropertyValueAttribute), true);
                    foreach (object o in attrs)
                    {
                        if (((CachedPropertyValueAttribute)o).Enabled == true)
                        {
                            propList.Add(i);
                        }
                    }
                }
            }
            LoadCache();
        }
Exemple #2
0
        public int CompareData(string aField, object aComparer, object aObject)
        {
            PropertyInfo info = CacheType.GetProperty(aField);

            if (aField == null)
            {
                throw new Exception("Trying to sort with non existant property");
            }
            if ((aObject == null) || (aComparer == null))
            {
                throw new Exception("Trying to sort with null object:\n" +
                                    "  Comparer: " + aComparer + "\n" +
                                    "  Object: " + aObject);
            }
            if ((aObject.GetType() != CacheType) || (aComparer.GetType() != CacheType))
            {
                throw new Exception("Trying to sort with wrong object");
            }

            if (info.PropertyType is IComparable)
            {
                return((info.GetValue(aComparer, null) as IComparable).CompareTo(info.GetValue(aObject, null)));
            }

            if ((info.PropertyType == typeof(int)) || (info.PropertyType == typeof(System.Int32)))
            {
                int a = (int)info.GetValue(aComparer, null);
                int b = (int)info.GetValue(aObject, null);
                return(CacheList.CompareData(a, b));
            }
            if ((info.PropertyType is string) || (info.PropertyType == typeof(System.String)))
            {
                string a = (string)info.GetValue(aComparer, null);
                string b = (string)info.GetValue(aObject, null);
                return(CacheList.CompareData(a, b));
            }
            if (info.PropertyType is DateTime)
            {
                DateTime a = (DateTime)info.GetValue(aComparer, null);
                DateTime b = (DateTime)info.GetValue(aObject, null);
                return(CacheList.CompareData(a, b));
            }
            throw new Exception("Wrong type comparision: " + info.PropertyType);
        }