Exemple #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 = ReflectedCache.TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

            return(cacheType.GetAttributes <TAttribute>(property, predicate));
        }
Exemple #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 = ReflectedCache.TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

            return(predicate == null
                ? cacheType.GetProperties()
                : cacheType.GetProperties(predicate));
        }
Exemple #3
0
        /// <summary>
        /// Gets the type of the attributes for.
        /// </summary>
        /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
        /// <typeparam name="TType">The type of the type.</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, TType>(TType type, PropertyInfo property = null, Func <TAttribute, bool> predicate = null)
            where TAttribute : Attribute
            where TType : class
        {
            Type           desiredType = type == null ? typeof(TType) : type.GetType();
            CachedTypeData cacheType   = ReflectedCache.TypePropertyCache.GetOrAdd(desiredType, new CachedTypeData(desiredType));

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

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

            cacheType = new CachedTypeData(type);
            ReflectedCache.TypePropertyCache.TryAdd(type, cacheType);
        }
Exemple #5
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();

            ReflectedCache.TypePropertyCache.TryGetValue(itemType, out cacheType);

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

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

            return(cacheType.Type);
        }
Exemple #6
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 = ReflectedCache.TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

            return(cacheType.GetProperty(predicate));
        }
Exemple #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 = ReflectedCache.TypePropertyCache.GetOrAdd(type, new CachedTypeData(type));

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