MethodHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright 2015-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. */
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Linq.Expressions;
  20. using System.Reflection;
  21. using MongoDB.Driver.Core.Misc;
  22. namespace MongoDB.Driver.Linq
  23. {
  24. internal static class MethodHelper
  25. {
  26. public static MethodInfo GetMethodInfo<TResult>(Expression<Func<TResult>> lambda)
  27. {
  28. Ensure.IsNotNull(lambda, nameof(lambda));
  29. switch (lambda.Body.NodeType)
  30. {
  31. case ExpressionType.Call:
  32. return ((MethodCallExpression)lambda.Body).Method;
  33. }
  34. throw new MongoInternalException(string.Format("Unable to extract method info from {0}", lambda.Body.ToString()));
  35. }
  36. public static MethodInfo GetMethodInfo<T, TResult>(Expression<Func<T, TResult>> lambda)
  37. {
  38. Ensure.IsNotNull(lambda, nameof(lambda));
  39. switch (lambda.Body.NodeType)
  40. {
  41. case ExpressionType.Call:
  42. return ((MethodCallExpression)lambda.Body).Method;
  43. }
  44. throw new MongoInternalException(string.Format("Unable to extract method info from {0}", lambda.Body.ToString()));
  45. }
  46. public static MethodInfo GetMethodDefinition<TResult>(Expression<Func<TResult>> lambda)
  47. {
  48. var methodInfo = GetMethodInfo(lambda);
  49. return GetMethodDefinition(methodInfo);
  50. }
  51. public static MethodInfo GetMethodDefinition<T, TResult>(Expression<Func<T, TResult>> lambda)
  52. {
  53. var methodInfo = GetMethodInfo(lambda);
  54. return GetMethodDefinition(methodInfo);
  55. }
  56. public static IEnumerable<MethodInfo> GetEnumerableAndQueryableMethodDefinitions(string name)
  57. {
  58. return typeof(Enumerable)
  59. .GetTypeInfo().GetMethods()
  60. .Concat(typeof(Queryable).GetTypeInfo().GetMethods())
  61. .Concat(typeof(MongoEnumerable).GetTypeInfo().GetMethods())
  62. .Concat(typeof(MongoQueryable).GetTypeInfo().GetMethods())
  63. .Where(x => x.Name == name)
  64. .Select(x => GetMethodDefinition(x));
  65. }
  66. public static MethodInfo GetMethodDefinition(MethodInfo methodInfo)
  67. {
  68. if (methodInfo.IsGenericMethod && !methodInfo.IsGenericMethodDefinition)
  69. {
  70. methodInfo = methodInfo.GetGenericMethodDefinition();
  71. }
  72. methodInfo = methodInfo.GetBaseDefinition();
  73. if (!methodInfo.DeclaringType.GetTypeInfo().IsGenericType)
  74. {
  75. return methodInfo;
  76. }
  77. var declaringTypeDefinition = methodInfo.DeclaringType.GetGenericTypeDefinition();
  78. #if NETSTANDARD1_5 || NETSTANDARD1_6 || NETCOREAPP
  79. var bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
  80. var parameterTypes = methodInfo.GetParameters().Select(p => p.ParameterType).ToArray();
  81. return declaringTypeDefinition.GetTypeInfo().GetMethods(bindingFlags)
  82. .Where(m => m.Name == methodInfo.Name && m.GetParameters().Count() == parameterTypes.Length) // TODO: need better matching
  83. .Single();
  84. #else
  85. return (MethodInfo)MethodBase.GetMethodFromHandle(methodInfo.MethodHandle, declaringTypeDefinition.TypeHandle);
  86. #endif
  87. }
  88. }
  89. }