2 // QueryableTransformer.cs
5 // Roei Erez (roeie@mainsoft.com)
6 // Jb Evain (jbevain@novell.com)
8 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Collections.Generic;
33 using System.Collections.ObjectModel;
35 using System.Linq.Expressions;
36 using System.Reflection;
37 using System.Runtime.CompilerServices;
39 namespace System.Linq {
41 class QueryableTransformer : ExpressionTransformer {
43 protected override MethodCallExpression VisitMethodCall (MethodCallExpression methodCall)
45 if (IsQueryableExtension (methodCall.Method))
46 return ReplaceQueryableMethod (methodCall);
48 return base.VisitMethodCall (methodCall);
51 protected override LambdaExpression VisitLambda (LambdaExpression lambda)
56 protected override ConstantExpression VisitConstant (ConstantExpression constant)
58 var qe = constant.Value as IQueryableEnumerable;
62 return Expression.Constant (qe.GetEnumerable ());
65 static bool IsQueryableExtension (MethodInfo method)
67 return HasExtensionAttribute (method) &&
68 method.GetParameters () [0].ParameterType.IsAssignableTo (typeof (IQueryable));
71 static bool HasExtensionAttribute (MethodInfo method)
73 return method.GetCustomAttributes (typeof (ExtensionAttribute), false).Length > 0;
76 MethodCallExpression ReplaceQueryableMethod (MethodCallExpression old)
78 Expression target = null;
79 if (old.Object != null)
80 target = Visit (old.Object);
82 var method = ReplaceQueryableMethod (old.Method);
83 var parameters = method.GetParameters ();
84 var arguments = new Expression [old.Arguments.Count];
86 for (int i = 0; i < arguments.Length; i++) {
87 arguments [i] = UnquoteIfNeeded (
88 Visit (old.Arguments [i]),
89 parameters [i].ParameterType);
92 return new MethodCallExpression (target, method, arguments.ToReadOnlyCollection ());
95 static Expression UnquoteIfNeeded (Expression expression, Type delegateType)
97 if (expression.NodeType != ExpressionType.Quote)
100 var lambda = (LambdaExpression) ((UnaryExpression) expression).Operand;
101 if (lambda.Type == delegateType)
107 static Type GetTargetDeclaringType (MethodInfo method)
109 return method.DeclaringType == typeof (Queryable) ? typeof (Enumerable) : method.DeclaringType;
112 static MethodInfo ReplaceQueryableMethod (MethodInfo method)
114 var result = GetMatchingMethod (method, GetTargetDeclaringType (method));
119 throw new InvalidOperationException (
121 "There is no method {0} on type {1} that matches the specified arguments",
123 method.DeclaringType.FullName));
126 static MethodInfo GetMatchingMethod (MethodInfo method, Type declaring)
128 foreach (var candidate in declaring.GetMethods ()) {
129 if (!MethodMatch (candidate, method))
132 if (method.IsGenericMethod)
133 return candidate.MakeGenericMethodFrom (method);
141 static bool MethodMatch (MethodInfo candidate, MethodInfo method)
143 if (candidate.Name != method.Name)
146 if (!HasExtensionAttribute (candidate))
149 var parameters = method.GetParameterTypes ();
151 if (parameters.Length != candidate.GetParameters ().Length)
154 if (method.IsGenericMethod) {
155 if (!candidate.IsGenericMethod)
158 if (candidate.GetGenericArguments ().Length != method.GetGenericArguments ().Length)
161 candidate = candidate.MakeGenericMethodFrom (method);
164 if (!TypeMatch (candidate.ReturnType, method.ReturnType))
167 var candidate_parameters = candidate.GetParameterTypes ();
169 if (candidate_parameters [0] != GetComparableType (parameters [0]))
172 for (int i = 1; i < candidate_parameters.Length; ++i)
173 if (!TypeMatch (candidate_parameters [i], parameters [i]))
179 static bool TypeMatch (Type candidate, Type type)
181 if (candidate == type)
184 return candidate == GetComparableType (type);
187 static Type GetComparableType (Type type)
189 if (type.IsGenericInstanceOf (typeof (IQueryable<>)))
190 type = typeof (IEnumerable<>).MakeGenericTypeFrom (type);
191 else if (type.IsGenericInstanceOf (typeof (IOrderedQueryable<>)))
192 type = typeof (IOrderedEnumerable<>).MakeGenericTypeFrom (type);
193 else if (type.IsGenericInstanceOf (typeof (Expression<>)))
194 type = type.GetFirstGenericArgument ();
195 else if (type == typeof (IQueryable))
196 type = typeof (IEnumerable);