private static Comparison <T> attributeParameterCountSort <T>(IReadOnlyCollection <Type> attributeTypes) where T : MethodBase { int Sort(T x, T y) => ServiceConstructorMethods.hasAttributePredicate <T>(attributeTypes)(x) ? ServiceConstructorMethods.hasAttributePredicate <T>(attributeTypes)(y) ? -x.GetParameters().Length.CompareTo(y.GetParameters().Length) : -1 : ServiceConstructorMethods.hasAttributePredicate <T>(attributeTypes)(y) ? 1 : -x.GetParameters().Length.CompareTo(y.GetParameters().Length); return(Sort); }
/// <summary> /// Utility method returns all Public and NonPublic constructors, /// sorted with the most parameters first. /// </summary> private static IEnumerable <ConstructorInfo> findConstructors( Type type, bool?requireAttributes, IReadOnlyCollection <Type> attributeTypes, ISequence <object> traceStack) { List <ConstructorInfo> constructors = type.GetConstructors( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .ToList(); if (constructors.Count == 0) { traceStack.Add($"No constructors found for '{type.GetFriendlyFullName()}'."); } if (requireAttributes == false) { constructors.Sort(ServiceConstructorMethods.attributeParameterCountSort <ConstructorInfo>(attributeTypes)); return(constructors); } if ((requireAttributes == true) || (constructors.FindIndex( ServiceConstructorMethods.hasAttributePredicate <ConstructorInfo>(attributeTypes)) >= 0)) { constructors.RemoveAll(ServiceConstructorMethods.hasAttributePredicate <ConstructorInfo>(attributeTypes, true)); } if (constructors.Count == 0) { traceStack.Add( $"No selected constructors found for '{type.GetFriendlyFullName()}'" + $" --- {attributeTypes.ToStringCollection(256, t => t.GetFriendlyFullName())}."); } constructors.Sort( ServiceConstructorMethods .attributeParameterCountSort <ConstructorInfo>(attributeTypes)); return(constructors); }