QueryableTransformer.cs 5.5 KB

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