PageRenderTime 70ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Core/System.Linq/QueryableTransformer.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 201 lines | 132 code | 41 blank | 28 comment | 40 complexity | 6358ac9edc334b8d7c83524e6c2e344b MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // QueryableTransformer.cs
  3. //
  4. // Authors:
  5. // Roei Erez (roeie@mainsoft.com)
  6. // Jb Evain (jbevain@novell.com)
  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 new MethodCallExpression (target, method, arguments.ToReadOnlyCollection ());
  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 result = GetMatchingMethod (method, GetTargetDeclaringType (method));
  96. if (result != null)
  97. return result;
  98. throw new InvalidOperationException (
  99. string.Format (
  100. "There is no method {0} on type {1} that matches the specified arguments",
  101. method.Name,
  102. method.DeclaringType.FullName));
  103. }
  104. static MethodInfo GetMatchingMethod (MethodInfo method, Type declaring)
  105. {
  106. foreach (var candidate in declaring.GetMethods ()) {
  107. if (!MethodMatch (candidate, method))
  108. continue;
  109. if (method.IsGenericMethod)
  110. return candidate.MakeGenericMethodFrom (method);
  111. return candidate;
  112. }
  113. return null;
  114. }
  115. static bool MethodMatch (MethodInfo candidate, MethodInfo method)
  116. {
  117. if (candidate.Name != method.Name)
  118. return false;
  119. if (!HasExtensionAttribute (candidate))
  120. return false;
  121. var parameters = method.GetParameterTypes ();
  122. if (parameters.Length != candidate.GetParameters ().Length)
  123. return false;
  124. if (method.IsGenericMethod) {
  125. if (!candidate.IsGenericMethod)
  126. return false;
  127. if (candidate.GetGenericArguments ().Length != method.GetGenericArguments ().Length)
  128. return false;
  129. candidate = candidate.MakeGenericMethodFrom (method);
  130. }
  131. if (!TypeMatch (candidate.ReturnType, method.ReturnType))
  132. return false;
  133. var candidate_parameters = candidate.GetParameterTypes ();
  134. if (candidate_parameters [0] != GetComparableType (parameters [0]))
  135. return false;
  136. for (int i = 1; i < candidate_parameters.Length; ++i)
  137. if (!TypeMatch (candidate_parameters [i], parameters [i]))
  138. return false;
  139. return true;
  140. }
  141. static bool TypeMatch (Type candidate, Type type)
  142. {
  143. if (candidate == type)
  144. return true;
  145. return candidate == GetComparableType (type);
  146. }
  147. static Type GetComparableType (Type type)
  148. {
  149. if (type.IsGenericInstanceOf (typeof (IQueryable<>)))
  150. type = typeof (IEnumerable<>).MakeGenericTypeFrom (type);
  151. else if (type.IsGenericInstanceOf (typeof (IOrderedQueryable<>)))
  152. type = typeof (IOrderedEnumerable<>).MakeGenericTypeFrom (type);
  153. else if (type.IsGenericInstanceOf (typeof (Expression<>)))
  154. type = type.GetFirstGenericArgument ();
  155. else if (type == typeof (IQueryable))
  156. type = typeof (IEnumerable);
  157. return type;
  158. }
  159. }
  160. }