/// <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)); }
/// <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)); }
/// <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)); }
/// <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); }
/// <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); }
/// <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)); }
/// <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)); }