示例#1
0
        public bool Compare <T>(T first, T second)
            where T : class
        {
            if ((first == null && second != null) ||
                (first != null && second == null))
            {
                return(false);
            }
            else if (first != null && second != null)
            {
                PropertyInfo[] properties = first.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

                object propertyValueFirst, propertyValueSecond;
                Type   propertyType;
                foreach (PropertyInfo property in properties)
                {
                    if (property.CanRead)
                    {
                        propertyValueFirst  = property.GetValue(first, null);
                        propertyValueSecond = property.GetValue(second, null);

                        propertyType = property.PropertyType;

                        if (ReflectionGuard.IsPrimitiveType(propertyType))
                        {
                            if (!_primitiveComparer.Compare(propertyValueFirst, propertyValueSecond))
                            {
                                return(false);
                            }
                        }
                        else if (ReflectionGuard.IsEnumerableType(propertyType))
                        {
                            if (!_enumerationComparer.Compare(this, propertyValueFirst, propertyValueSecond))
                            {
                                return(false);
                            }
                        }
                        else if (ReflectionGuard.IsClass(propertyType))
                        {
                            if (!this.Compare(propertyValueFirst, propertyValueSecond))
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
示例#2
0
        public bool Set <T>(string key, T data)
        {
            var duration    = ReflectionGuard.GetDuration(data);
            var transaction = _RedisDb.CreateTransaction();

            transaction.HashSetAsync(key, _dataConverter.Serialize(key), _dataConverter.Serialize(data));
            transaction.KeyExpireAsync(key, duration);
            var execute = transaction.ExecuteAsync();

            _RedisDb.Wait(execute);

            return(execute.Result);
        }
        public bool Compare(IUserComparer userComparer, object first, object second)
        {
            if ((first == null && second != null) ||
                (first != null && second == null))
            {
                return(false);
            }
            else if (first != null && second != null)
            {
                IEnumerable <object> enumerableFirst, enumerableSecond;
                enumerableFirst  = ((IEnumerable)first).Cast <object>();
                enumerableSecond = ((IEnumerable)second).Cast <object>();


                if (enumerableFirst.Count() != enumerableSecond.Count())
                {
                    return(false);
                }
                else
                {
                    int count = enumerableFirst.Count();

                    object itemFirst, itemSecond;
                    Type   itemType;
                    for (int index = 0; index < count; index++)
                    {
                        itemFirst  = enumerableFirst.ElementAt(index);
                        itemSecond = enumerableSecond.ElementAt(index);
                        itemType   = itemFirst.GetType();

                        if (ReflectionGuard.IsPrimitiveType(itemType))
                        {
                            if (!_primitiveComparer.Compare(itemFirst, itemSecond))
                            {
                                return(false);
                            }
                        }
                        else if (!userComparer.Compare(itemFirst, itemSecond))
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }