QueryableTransformer.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // QueryableTransformer.cs
  3. //
  4. // Authors:
  5. // Roei Erez ([email protected])
  6. // Jb Evain ([email protected])
  7. //
  8. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  9. //
  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:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  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.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Collections.ObjectModel;
  33. using System.Linq;
  34. using System.Linq.Expressions;
  35. using System.Reflection;
  36. using System.Runtime.CompilerServices;
  37. namespace System.Linq {
  38. class QueryableTransformer : ExpressionTransformer {
  39. protected override Expression VisitMethodCall (MethodCallExpression methodCall)
  40. {
  41. if (IsQueryableExtension (methodCall.Method))
  42. return ReplaceQueryableMethod (methodCall);
  43. return base.VisitMethodCall (methodCall);
  44. }
  45. protected override Expression VisitLambda (LambdaExpression lambda)
  46. {
  47. return lambda;
  48. }
  49. protected override Expression VisitConstant (ConstantExpression constant)
  50. {
  51. var qe = constant.Value as IQueryableEnumerable;
  52. if (qe == null)
  53. return constant;
  54. var enumerable = qe.GetEnumerable ();
  55. if (enumerable != null)
  56. return Expression.Constant (enumerable);
  57. return Visit (qe.Expression);
  58. }
  59. static bool IsQueryableExtension (MethodInfo method)
  60. {
  61. return HasExtensionAttribute (method) &&
  62. method.GetParameters () [0].ParameterType.IsAssignableTo (typeof (IQueryable));
  63. }
  64. static bool HasExtensionAttribute (MethodInfo method)
  65. {
  66. return method.GetCustomAttributes (typeof (ExtensionAttribute), false).Length > 0;
  67. }
  68. MethodCallExpression ReplaceQueryableMethod (MethodCallExpression old)
  69. {
  70. Expression target = null;
  71. if (old.Object != null)
  72. target = Visit (old.Object);
  73. var method = ReplaceQueryableMethod (old.Method);
  74. var parameters = method.GetParameters ();
  75. var arguments = new Expression [old.Arguments.Count];
  76. for (int i = 0; i < arguments.Length; i++) {
  77. arguments [i] = UnquoteIfNeeded (
  78. Visit (old.Arguments [i]),
  79. parameters [i].ParameterType);
  80. }
  81. return Expression.Call (target, method, arguments);
  82. }
  83. static Expression UnquoteIfNeeded (Expression expression, Type delegateType)
  84. {
  85. if (expression.NodeType != ExpressionType.Quote)
  86. return expression;
  87. var lambda = (LambdaExpression) ((UnaryExpression) expression).Operand;
  88. if (lambda.Type == delegateType)
  89. return lambda;
  90. return expression;
  91. }
  92. static Type GetTargetDeclaringType (MethodInfo method)
  93. {
  94. return method.DeclaringType == typeof (Queryable) ? typeof (Enumerable) : method.DeclaringType;
  95. }
  96. static MethodInfo ReplaceQueryableMethod (MethodInfo method)
  97. {
  98. var target_type = GetTargetDeclaringType (method);
  99. var result = GetMatchingMethod (method, target_type);
  100. if (result != null)
  101. return result;
  102. throw new InvalidOperationException (
  103. string.Format (
  104. "There is no method {0} on type {1} that matches the specified arguments",
  105. method.Name,
  106. target_type.FullName));
  107. }
  108. static MethodInfo GetMatchingMethod (MethodInfo method, Type declaring)
  109. {
  110. foreach (var candidate in declaring.GetMethods ()) {
  111. if (!MethodMatch (candidate, method))
  112. continue;
  113. if (method.IsGenericMethod)
  114. return candidate.MakeGenericMethodFrom (method);
  115. return candidate;
  116. }
  117. return null;
  118. }
  119. static bool MethodMatch (MethodInfo candidate, MethodInfo method)
  120. {
  121. if (candidate.Name != method.Name)
  122. return false;
  123. if (!HasExtensionAttribute (candidate))
  124. return false;
  125. var parameters = method.GetParameterTypes ();
  126. if (parameters.Length != candidate.GetParameters ().Length)
  127. return false;
  128. if (method.IsGenericMethod) {
  129. if (!candidate.IsGenericMethod)
  130. return false;
  131. if (candidate.GetGenericArguments ().Length != method.GetGenericArguments ().Length)
  132. return false;
  133. candidate = candidate.MakeGenericMethodFrom (method);
  134. }
  135. if (!TypeMatch (candidate.ReturnType, method.ReturnType))
  136. return false;
  137. var candidate_parameters = candidate.GetParameterTypes ();
  138. if (candidate_parameters [0] != GetComparableType (parameters [0]))
  139. return false;
  140. for (int i = 1; i < candidate_parameters.Length; ++i)
  141. if (!TypeMatch (candidate_parameters [i], parameters [i]))
  142. return false;
  143. return true;
  144. }
  145. static bool TypeMatch (Type candidate, Type type)
  146. {
  147. if (candidate == type)
  148. return true;
  149. return candidate == GetComparableType (type);
  150. }
  151. static Type GetComparableType (Type type)
  152. {
  153. if (type.IsGenericInstanceOf (typeof (IQueryable<>)))
  154. type = typeof (IEnumerable<>).MakeGenericTypeFrom (type);
  155. else if (type.IsGenericInstanceOf (typeof (IOrderedQueryable<>)))
  156. type = typeof (IOrderedEnumerable<>).MakeGenericTypeFrom (type);
  157. else if (type.IsGenericInstanceOf (typeof (Expression<>)))
  158. type = type.GetFirstGenericArgument ();
  159. else if (type == typeof (IQueryable))
  160. type = typeof (IEnumerable);
  161. return type;
  162. }
  163. }
  164. }