/// <summary> /// Resolves class types inheriting from or implementing the specified type and marked with the specified attribute. /// </summary> /// <typeparam name="T">The type to inherit from or implement.</typeparam> /// <typeparam name="TAttribute">The type of the attribute.</typeparam> /// <param name="cache">Indicates whether to use cache for type resolution.</param> /// <param name="specificAssemblies">A set of assemblies for type resolution.</param> /// <returns>All class types inheriting from or implementing the specified type and marked with the specified attribute.</returns> /// <remarks>Caching is disabled when using specific assemblies.</remarks> public IEnumerable <Type> ResolveTypesWithAttribute <T, TAttribute>(bool cache = true, IEnumerable <Assembly> specificAssemblies = null) where TAttribute : Attribute { // do not cache anything from specific assemblies cache &= specificAssemblies == null; // if not caching, or not IDiscoverable, directly resolve types if (cache == false || typeof(IDiscoverable).IsAssignableFrom(typeof(T)) == false) { return(ResolveTypesInternal( typeof(T), typeof(TAttribute), () => TypeFinder.FindClassesOfTypeWithAttribute <T, TAttribute>(specificAssemblies ?? AssembliesToScan), cache)); } // if caching and IDiscoverable // filter the cached discovered types (and cache the result) var discovered = ResolveTypesInternal( typeof(IDiscoverable), null, () => TypeFinder.FindClassesOfType <IDiscoverable>(AssembliesToScan), true); return(ResolveTypesInternal( typeof(T), typeof(TAttribute), () => discovered .Where(x => typeof(T).IsAssignableFrom(x)) .Where(x => x.GetCustomAttributes <TAttribute>(false).Any()), true)); }
/// <summary> /// Generic method to find the specified type that has an attribute and cache the result /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TAttribute"></typeparam> /// <returns></returns> public IEnumerable <Type> ResolveTypesWithAttribute <T, TAttribute>(bool cacheResult = true, IEnumerable <Assembly> specificAssemblies = null) where TAttribute : Attribute { return(ResolveTypes <T>( () => TypeFinder.FindClassesOfTypeWithAttribute <T, TAttribute>(specificAssemblies ?? AssembliesToScan), TypeResolutionKind.FindTypesWithAttribute, cacheResult)); }
/// <summary> /// Generic method to find the specified type that has an attribute and cache the result /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TAttribute"></typeparam> /// <returns></returns> internal IEnumerable <Type> ResolveTypesWithAttribute <T, TAttribute>(bool cacheResult = true) where TAttribute : Attribute { return(ResolveTypes <T>( () => TypeFinder.FindClassesOfTypeWithAttribute <T, TAttribute>(AssembliesToScan), TypeResolutionKind.FindTypesWithAttribute, cacheResult)); }