private static IOrderedEnumerable <TItem> Sort <TItem>(IEnumerable <TItem> query, string propertyName, bool orderByAsc) { (var expression, var property) = GetSortExpression <TItem>(propertyName); //compileMethodInfo can not be cached in static field because Compile is method of generic Expression<> type var compileMethodInfo = TypeInfoCache.GetPublicMethods(expression.GetType()) .First(x => x.Name == "Compile" && x.GetParameters().Length == 0); var func = compileMethodInfo.Invoke(expression, null); var methodInfo = orderByAsc ? EnumerableOrderByMethodInfo : EnumerableOrderByDescendingMethodInfo; var orderBy = methodInfo.MakeGenericMethod(typeof(TItem), property.PropertyType); return((IOrderedEnumerable <TItem>)orderBy.Invoke(query, new object[] { query, func })); }
private static FilterProperty[] CalcFilterProperties(Type type) { var props = TypeInfoCache .GetPublicProperties(type) .Where(x => x.GetCustomAttribute <NotAutoFilteredAttribute>() == null) .Select(x => new FilterProperty { PropertyInfo = x, FilterPropertyAttribute = x.GetCustomAttribute <FilterPropertyAttribute>() }) .ToArray(); //each property must have an attribute to generate an expression foreach (var x in props) { if (x.FilterPropertyAttribute == null) { x.FilterPropertyAttribute = new FilterPropertyAttribute(); } } return(props); }