private static IEnumerable<Type> GetQueryHandlers(Reflection.Assembly assembly) { return from t in assembly.GetTypes() from i in t.GetInterfaces() where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IQueryHandler<>) select t; }
private InjecteeCollection GetInjectees(SR.Assembly targetAssembly, SR.Assembly injectionAssembly) { _classInjectees = new List<TypeInjecteeBase>(); var injectees = new InjecteeCollection(); var injectionAssemblyTypes = injectionAssembly.GetTypes(); foreach (var injectionType in injectionAssemblyTypes) { var classAttrs = injectionType.GetCustomAttributes(typeof(InjectionAttributeBase), false); if (classAttrs.Count(a => a != null) >= 2) throw new InvalidOperationException("Can not define multiple class attributes for the same class"); var classAttr = classAttrs.Select(a => a as ITypeAttribute).FirstOrDefault(a => a != null); if (classAttr == null) continue; if (classAttr is TypeAddInjectionAttribute) { var classAddInjectee = (TypeAddInjectee)classAttr.GetInjectee(null, injectionType); if (!String.IsNullOrEmpty(classAddInjectee.Attr.ParentFullName)) classAddInjectee.ParentType = targetAssembly.GetType(classAddInjectee.Attr.ParentFullName, true); injectees.Add(classAddInjectee); _classInjectees.Add(classAddInjectee); } else { var name = !String.IsNullOrEmpty(classAttr.FullName) ? classAttr.FullName : injectionType.GetNormalizedName(); var targetType = targetAssembly.GetType(name, true); var baseClassInjectee = classAttr.GetInjectee(targetType, injectionType); injectees.Add(baseClassInjectee); _classInjectees.Add(baseClassInjectee); } } foreach (var baseClassInjectee in _classInjectees) { var classInjectee = baseClassInjectee as TypeInjectee; if (classInjectee != null) { foreach (var field in classInjectee.InjecteeType.GetFields(BINDING_FLAGS)) { var fieldAttrs = field.GetCustomAttributes(typeof(InjectionAttributeBase), false); if (fieldAttrs.Count(a => a != null) >= 2) throw new InvalidOperationException("Can not define multiple field attributes for the same field"); var fieldAttr = fieldAttrs.Select(a => a as IFieldAttribute).FirstOrDefault(a => a != null); if (fieldAttr == null) continue; var targetField = GetTargetField(fieldAttr, classInjectee.TargetType, field); var fieldInjectee = fieldAttr.GetInjectee(targetField, field, classInjectee); classInjectee.Injectees.Add(fieldInjectee); } foreach (var method in classInjectee.InjecteeType.GetMethods(BINDING_FLAGS)) { var methodAttrs = method.GetCustomAttributes(typeof(InjectionAttributeBase), false); if (methodAttrs.Count(a => a != null) >= 2) throw new InvalidOperationException("Can not define multiple method attributes for the same method"); var methodAttr = methodAttrs.Select(a => a as IMethodAttribute).FirstOrDefault(a => a != null); if (methodAttr == null) continue; var targetMethod = GetTargetMethod(methodAttr, classInjectee.TargetType, method); var methodInjectee = methodAttr.GetInjectee(targetMethod, method, classInjectee); classInjectee.Injectees.Add(methodInjectee); } } } return injectees; }
private static Type[] GetTypes(SR.Assembly assembly) { IEnumerable<Type> types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException e) { types = e.Types; } return types.Where(t => t != null).ToArray(); }