Exemple #1
0
        /// <summary>
        /// Get properties, cached for a type
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> GetProps(object value)
        {
            var type = value.GetType();
            KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> props;

            if (_propsCache.TryGetValue(type, out props))
            {
                if (props.Key.Length != 0 && props.Value.Length == 0)
                {
                    var lateBoundMethods = new ReflectionHelpers.LateBoundMethod[props.Key.Length];
                    for (int i = 0; i < props.Key.Length; i++)
                    {
                        lateBoundMethods[i] = ReflectionHelpers.CreateLateBoundMethod(props.Key[i].GetGetMethod());
                    }
                    props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(props.Key, lateBoundMethods);
                    _propsCache.TryAddValue(type, props);
                }
                return(props);
            }

            PropertyInfo[] properties = null;

            try
            {
                properties = type.GetProperties(PublicProperties);
            }
            catch (Exception ex)
            {
                InternalLogger.Warn(ex, "Failed to get JSON properties for type: {0}", type);
            }
            finally
            {
                if (properties == null)
                {
                    properties = ArrayHelper.Empty <PropertyInfo>();
                }
            }

            // Skip Index-Item-Properties (Ex. public string this[int Index])
            foreach (var prop in properties)
            {
                if (!prop.CanRead || prop.GetIndexParameters().Length != 0 || prop.GetGetMethod() == null)
                {
                    properties = properties.Where(p => p.CanRead && p.GetIndexParameters().Length == 0 && p.GetGetMethod() != null).ToArray();
                    break;
                }
            }

            props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(properties, ArrayHelper.Empty <ReflectionHelpers.LateBoundMethod>());
            _propsCache.TryAddValue(type, props);
            return(props);
        }
Exemple #2
0
        public void RecentlyUsedLookupTest()
        {
            string value;

            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
                for (int j = 0; j < i; j += 10)
                {
                    Assert.True(mruCache.TryGetValue(j, out value));
                    Assert.Equal(j.ToString(), value);
                }
            }

            for (int j = 0; j < 100; j += 10)
            {
                Assert.True(mruCache.TryGetValue(j, out value));
                Assert.Equal(j.ToString(), value);
            }

            for (int i = 170; i < 200; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }
        }
Exemple #3
0
 private void AppendEnumAsString(StringBuilder sb, Enum value)
 {
     if (!_enumCache.TryGetValue(value, out var textValue))
     {
         textValue = value.ToString();
         _enumCache.TryAddValue(value, textValue);
     }
     sb.Append(textValue);
 }
        /// <summary>
        /// Get properties, cached for a type
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> GetProps(object value)
        {
            var type = value.GetType();
            KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> props;

            if (_propsCache.TryGetValue(type, out props))
            {
                if (props.Key.Length != 0 && props.Value.Length == 0)
                {
                    var lateBoundMethods = new ReflectionHelpers.LateBoundMethod[props.Key.Length];
                    for (int i = 0; i < props.Key.Length; i++)
                    {
                        lateBoundMethods[i] = ReflectionHelpers.CreateLateBoundMethod(props.Key[i].GetGetMethod());
                    }
                    props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(props.Key, lateBoundMethods);
                    _propsCache.TryAddValue(type, props);
                }
                return(props);
            }

            PropertyInfo[] properties = null;

            try
            {
                properties = type.GetProperties(PublicProperties);
            }
            catch (Exception ex)
            {
                Common.InternalLogger.Warn(ex, "Failed to get JSON properties for type: {0}", type);
            }
            finally
            {
                if (properties == null)
                {
                    properties = ArrayHelper.Empty <PropertyInfo>();
                }
            }

            props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(properties, ArrayHelper.Empty <ReflectionHelpers.LateBoundMethod>());
            _propsCache.TryAddValue(type, props);
            return(props);
        }
        private string EnumAsString(Enum value)
        {
            string textValue;

            if (!_enumCache.TryGetValue(value, out textValue))
            {
                textValue = Convert.ToString(value, CultureInfo.InvariantCulture);
                _enumCache.TryAddValue(value, textValue);
            }
            return(textValue);
        }
Exemple #6
0
        public void OverflowFreshCacheAndLookupTest()
        {
            string value;
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
                Assert.True(mruCache.TryGetValue(i, out value));    // No longer a virgin
                Assert.Equal(i.ToString(), value);
            }

            for (int j = 0; j < 2; ++j)
            {
                for (int i = 110; i < 200; ++i)
                {
                    if (!mruCache.TryGetValue(i, out value))
                    {
                        mruCache.TryAddValue(i, i.ToString());
                        Assert.True(mruCache.TryGetValue(i, out value));
                    }
                }
            }

            for (int i = 300; i < 310; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
            }

            int cacheCount = 0;

            for (int i = 110; i < 200; ++i)
            {
                if (mruCache.TryGetValue(i, out value))
                {
                    ++cacheCount;
                }
            }

            Assert.True(cacheCount > 60);   // See that old cache was not killed
        }
Exemple #7
0
        public void SimpleCacheAddAndLookupTest()
        {
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 100; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
            }

            string value;

            for (int i = 0; i < 100; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }

            Assert.False(mruCache.TryGetValue(101, out value));
        }
Exemple #8
0
        public void OverflowCacheAndLookupTest()
        {
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
            }

            string value;

            Assert.False(mruCache.TryGetValue(0, out value));
            Assert.False(mruCache.TryGetValue(90, out value));

            for (int i = 120; i < 200; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }
        }
Exemple #9
0
        public void OverflowVersionCacheAndLookupTest()
        {
            string value;
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
                Assert.True(mruCache.TryGetValue(i, out value));    // No longer a virgin
                Assert.Equal(i.ToString(), value);
            }

            for (int i = 0; i < 90; ++i)
            {
                Assert.False(mruCache.TryGetValue(i, out value));
            }

            for (int i = 140; i < 200; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }
        }