Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reflection helper method to set a property value
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        protected bool SetProperty(object instance, string name, object value)
        {
            var fastProp = _instanceType != null?FastProperty.GetProperty(_instanceType, name, PropertyCachingStrategy.EagerCached) : null;

            if (fastProp != null)
            {
                fastProp.SetValue(instance ?? this, value);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Checks whether a property exists in the Property collection
        /// or as a property on the instance
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="includeInstanceProperties"></param>
        /// <returns></returns>
        public bool Contains(string propertyName, bool includeInstanceProperties = false)
        {
            if (Properties.ContainsKey(propertyName))
            {
                return(true);
            }

            if (includeInstanceProperties && _instance != null)
            {
                return(FastProperty.GetProperties(_instance).ContainsKey(propertyName));
            }

            return(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns all properties
        /// </summary>
        /// <param name="includeInstanceProperties"></param>
        /// <returns></returns>
        public IEnumerable <KeyValuePair <string, object> > GetProperties(bool includeInstanceProperties = false)
        {
            foreach (var key in this.Properties.Keys)
            {
                yield return(new KeyValuePair <string, object>(key, this.Properties[key]));
            }

            if (includeInstanceProperties && _instance != null)
            {
                foreach (var prop in FastProperty.GetProperties(_instance).Values)
                {
                    if (!this.Properties.ContainsKey(prop.Name))
                    {
                        yield return(new KeyValuePair <string, object>(prop.Name, prop.GetValue(_instance)));
                    }
                }
            }
        }
Ejemplo n.º 6
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);
        }