Example #1
0
        /// <summary>
        /// Gets all attributes for a given Type.
        /// </summary>
        /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
        /// <param name="type">The type.</param>
        /// <param name="property">The property.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Returns a collection of Attributes</returns>
        public static IEnumerable <TAttribute> GetAttributes <TAttribute>(Type type, PropertyInfo property = null, Func <Attribute, bool> predicate = null)
            where TAttribute : Attribute
        {
            CachedTypeData cacheType = TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

            return(cacheType.GetAttributes <TAttribute>(property, predicate));
        }
Example #2
0
        /// <summary>
        /// Gets the properties for the provided Type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Returns a collection of PropertyInfo types</returns>
        public static IEnumerable <PropertyInfo> GetPropertiesForType(Type type, Func <PropertyInfo, bool> predicate = null)
        {
            CachedTypeData cacheType = TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

            return(predicate == null
                ? cacheType.GetProperties()
                : cacheType.GetProperties(predicate));
        }
Example #3
0
        /// <summary>
        /// Gets the type of the attribute for.
        /// </summary>
        /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
        /// <typeparam name="TType">The type of the type.</typeparam>
        /// <param name="property">The property.</param>
        /// <param name="type">The type.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Returns an Attribute</returns>
        public static TAttribute GetAttribute <TAttribute, TType>(PropertyInfo property = null, TType type = null, Func <TAttribute, bool> predicate = null)
            where TAttribute : Attribute
            where TType : class
        {
            Type           desiredType = type == null ? typeof(TType) : type.GetType();
            CachedTypeData cacheType   = TypePropertyCache.GetOrAdd(desiredType, new CachedTypeData(desiredType));

            return(cacheType.GetAttribute <TAttribute>(property, predicate));
        }
Example #4
0
        /// <summary>
        /// Adds the type.
        /// </summary>
        /// <param name="type">The type.</param>
        public static void AddType(Type type)
        {
            CachedTypeData cacheType;

            TypePropertyCache.TryGetValue(type, out cacheType);
            if (cacheType != null)
            {
                return;
            }

            cacheType = new CachedTypeData(type);
            TypePropertyCache.TryAdd(type, cacheType);
        }
Example #5
0
        /// <summary>
        /// Gets the property.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Returns a PropertyInfo</returns>
        public static PropertyInfo GetProperty(Type type, Func <PropertyInfo, bool> predicate)
        {
            CachedTypeData cacheType = TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

            try
            {
                return(cacheType.GetProperty(predicate));
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #6
0
        /// <summary>
        /// Gets the type.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <typeparam name="T">The Type to fetch from cache</typeparam>
        /// <returns>Returns a cached Type</returns>
        public static Type GetType <T>(T item) where T : class
        {
            CachedTypeData cacheType;
            Type           itemType = item.GetType();

            TypePropertyCache.TryGetValue(itemType, out cacheType);

            if (cacheType != null)
            {
                return(cacheType.Type);
            }

            cacheType = new CachedTypeData(itemType);
            TypePropertyCache.TryAdd(itemType, cacheType);

            return(cacheType.Type);
        }
Example #7
0
        /// <summary>
        /// Gets the single attribute for Type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="property">The property.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Returns an Attribute</returns>
        public static Attribute GetAttribute(Type type, PropertyInfo property = null, Func <Attribute, bool> predicate = null)
        {
            CachedTypeData cacheType = TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

            return(cacheType.GetAttribute(property, predicate));
        }