/// <summary> /// Find all types using <typeparamref name="TAttribute" />. /// </summary> /// <param name="typeDefined">Using inheritance or not.</param> /// <typeparam name="TAttribute"><see cref="System.Attribute" /> used by searched <see cref="Type" />s.</typeparam> /// <returns>List of all <see cref="Type" />s in an <see cref="IAppDomainAdapter" /> using <typeparamref name="TAttribute" />.</returns> protected IEnumerable <Type> GetTypesWith <TAttribute>(TypeDefined typeDefined) where TAttribute : System.Attribute { return(appDomain.GetAssemblies() .SelectMany(assembly => assembly.GetTypes()) .Where(type => type.IsDefined(typeof(TAttribute), typeDefined == TypeDefined.Inherit)) ); }
private IDictionary <Type, IList <Type> > FindAnnotatedTypes(TypeDefined typeDefined) { IEnumerable <Type> types = appDomain.GetAssemblies() .SelectMany(assembly => assembly.GetTypes()); IDictionary <Type, IList <Type> > typesPerAttribute = new Dictionary <Type, IList <Type> > { { typeof(RegisterTypeAttribute), new List <Type>() }, { typeof(RegisterProviderAttribute), new List <Type>() } }; foreach (Type classType in types) { // todo avoid nested loop #pragma warning disable AV1532 foreach (KeyValuePair <Type, IList <Type> > kv in typesPerAttribute) #pragma warning restore AV1532 { Type attributeType = kv.Key; IList <Type> typesWithAttribute = kv.Value; if (classType.IsDefined(attributeType, typeDefined == TypeDefined.Inherit)) { if (classType.IsStatic() || classType.IsAbstract) { throw new InvalidOperationException( $"Class type must not be static or abstract to be used with RegisterTypeAttribute: {classType.FullName}"); } typesWithAttribute.Add(classType); } } } return(typesPerAttribute); }