QueryableTransformer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. return Expression.Constant (qe.GetEnumerable ());
  55. }
  56. static bool IsQueryableExtension (MethodInfo method)
  57. {
  58. return HasExtensionAttribute (method) &&
  59. method.GetParameters () [0].ParameterType.IsAssignableTo (typeof (IQueryable));
  60. }
  61. static bool HasExtensionAttribute (MethodInfo method)
  62. {
  63. return method.GetCustomAttributes (typeof (ExtensionAttribute), false).Length > 0;
  64. }
  65. MethodCallExpression ReplaceQueryableMethod (MethodCallExpression old)
  66. {
  67. Expression target = null;
  68. if (old.Object != null)
  69. target = Visit (old.Object);
  70. var method = ReplaceQueryableMethod (old.Method);
  71. var parameters = method.GetParameters ();
  72. var arguments = new Expression [old.Arguments.Count];
  73. for (int i = 0; i < arguments.Length; i++) {
  74. arguments [i] = UnquoteIfNeeded (
  75. Visit (old.Arguments [i]),
  76. parameters [i].ParameterType);
  77. }
  78. return Expression.Call (target, method, arguments);
  79. }
  80. static Expression UnquoteIfNeeded (Expression expression, Type delegateType)
  81. {
  82. if (expression.NodeType != ExpressionType.Quote)
  83. return expression;
  84. var lambda = (LambdaExpression) ((UnaryExpression) expression).Operand;
  85. if (lambda.Type == delegateType)
  86. return lambda;
  87. return expression;
  88. }
  89. static Type GetTargetDeclaringType (MethodInfo method)
  90. {
  91. return method.DeclaringType == typeof (Queryable) ? typeof (Enumerable) : method.DeclaringType;
  92. }
  93. static MethodInfo ReplaceQueryableMethod (MethodInfo method)
  94. {
  95. var target_type = GetTargetDeclaringType (method);
  96. var result = GetMatchingMethod (method, target_type);
  97. if (result != null)
  98. return result;
  99. throw new InvalidOperationException (
  100. string.Format (
  101. "There is no method {0} on type {1} that matches the specified arguments",
  102. method.Name,
  103. target_type.FullName));
  104. }
  105. static MethodInfo GetMatchingMethod (MethodInfo method, Type declaring)
  106. {
  107. foreach (var candidate in declaring.GetMethods ()) {
  108. if (!MethodMatch (candidate, method))
  109. continue;
  110. if (method.IsGenericMethod)
  111. return candidate.MakeGenericMethodFrom (method);
  112. return candidate;
  113. }
  114. return null;
  115. }
  116. static bool MethodMatch (MethodInfo candidate, MethodInfo method)
  117. {
  118. if (candidate.Name != method.Name)
  119. return false;
  120. if (!HasExtensionAttribute (candidate))
  121. return false;
  122. var parameters = method.GetParameterTypes ();
  123. if (parameters.Length != candidate.GetParameters ().Length)
  124. return false;
  125. if (method.IsGenericMethod) {
  126. if (!candidate.IsGenericMethod)
  127. return false;
  128. if (candidate.GetGenericArguments ().Length != method.GetGenericArguments ().Length)
  129. return false;
  130. candidate = candidate.MakeGenericMethodFrom (method);
  131. }
  132. if (!TypeMatch (candidate.ReturnType, method.ReturnType))
  133. return false;
  134. var candidate_parameters = candidate.GetParameterTypes ();
  135. if (candidate_parameters [0] != GetComparableType (parameters [0]))
  136. return false;
  137. for (int i = 1; i < candidate_parameters.Length; ++i)
  138. if (!TypeMatch (candidate_parameters [i], parameters [i]))
  139. return false;
  140. return true;
  141. }
  142. static bool TypeMatch (Type candidate, Type type)
  143. {
  144. if (candidate == type)
  145. return true;
  146. return candidate == GetComparableType (type);
  147. }
  148. static Type GetComparableType (Type type)
  149. {
  150. if (type.IsGenericInstanceOf (typeof (IQueryable<>)))
  151. type = typeof (IEnumerable<>).MakeGenericTypeFrom (type);
  152. else if (type.IsGenericInstanceOf (typeof (IOrderedQueryable<>)))
  153. type = typeof (IOrderedEnumerable<>).MakeGenericTypeFrom (type);
  154. else if (type.IsGenericInstanceOf (typeof (Expression<>)))
  155. type = type.GetFirstGenericArgument ();
  156. else if (type == typeof (IQueryable))
  157. type = typeof (IEnumerable);
  158. return type;
  159. }
  160. }
  161. }