Merge pull request #2964 from ludovic-henry/sgen-monocontext
[mono.git] / mcs / class / referencesource / System.Web / Util / QueryableUtility.cs
1 using System.Linq;
2 using System.Linq.Expressions;
3 using System.Reflection;
4
5 namespace System.Web.Util {
6     internal static class QueryableUtility {
7         private static readonly string[] _orderMethods = new[] { "OrderBy", "ThenBy", "OrderByDescending", "ThenByDescending" };
8         private static readonly MethodInfo[] _methods = typeof(Queryable).GetMethods(BindingFlags.Public | BindingFlags.Static);
9
10         private static MethodInfo GetQueryableMethod(Expression expression) {
11             if (expression.NodeType == ExpressionType.Call) {
12                 var call = (MethodCallExpression)expression;
13                 if (call.Method.IsStatic && call.Method.DeclaringType == typeof(Queryable)) {
14                     return call.Method.GetGenericMethodDefinition();
15                 }
16             }
17             return null;
18         }
19
20         public static bool IsQueryableMethod(Expression expression, string method) {
21             return _methods.Where(m => m.Name == method).Contains(GetQueryableMethod(expression));
22         }
23
24         public static bool IsOrderingMethod(Expression expression) {
25             return _orderMethods.Any(method => IsQueryableMethod(expression, method));
26         }
27
28     }
29 }