Beispiel #1
0
        public static FastProperty GetProperty(
            PropertyInfo propertyInfo,
            PropertyCachingStrategy cachingStrategy = PropertyCachingStrategy.Cached)
        {
            Guard.ArgumentNotNull(() => propertyInfo);

            FastProperty fastProperty = null;

            if (TryGetCachedProperty(propertyInfo.ReflectedType, propertyInfo.Name, cachingStrategy == PropertyCachingStrategy.EagerCached, out fastProperty))
            {
                return(fastProperty);
            }

            var key = new PropertyKey(propertyInfo.ReflectedType, propertyInfo.Name);

            if (!_singlePropertiesCache.TryGetValue(key, out fastProperty))
            {
                fastProperty = CreateInstance(propertyInfo);
                if (cachingStrategy > PropertyCachingStrategy.Uncached)
                {
                    _singlePropertiesCache.TryAdd(key, fastProperty);
                }
            }

            return(fastProperty);
        }
Beispiel #2
0
        public static FastProperty GetProperty(
            Type type,
            string propertyName,
            PropertyCachingStrategy cachingStrategy = PropertyCachingStrategy.Cached)
        {
            Guard.ArgumentNotNull(() => type);
            Guard.ArgumentNotEmpty(() => propertyName);

            FastProperty fastProperty = null;

            if (TryGetCachedProperty(type, propertyName, cachingStrategy == PropertyCachingStrategy.EagerCached, out fastProperty))
            {
                return(fastProperty);
            }

            var key = new PropertyKey(type, propertyName);

            if (!_singlePropertiesCache.TryGetValue(key, out fastProperty))
            {
                var pi = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (pi != null)
                {
                    fastProperty = CreateInstance(pi);
                    if (cachingStrategy > PropertyCachingStrategy.Uncached)
                    {
                        _singlePropertiesCache.TryAdd(key, fastProperty);
                    }
                }
            }

            return(fastProperty);
        }
Beispiel #3
0
        private static bool TryGetCachedProperty(
            Type type,
            string propertyName,
            bool eagerCached,
            out FastProperty fastProperty)
        {
            fastProperty = null;
            IDictionary <string, FastProperty> allProperties;

            if (eagerCached)
            {
                allProperties = (IDictionary <string, FastProperty>)GetProperties(type);
                allProperties.TryGetValue(propertyName, out fastProperty);
            }

            if (fastProperty == null && _propertiesCache.TryGetValue(type, out allProperties))
            {
                allProperties.TryGetValue(propertyName, out fastProperty);
            }

            return(fastProperty != null);
        }