Skip to content

Commit b734e51

Browse files
committed
fix zzzprojects#157 - other way to search for the methods
1 parent 57e7c43 commit b734e51

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs

+32-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Reflection;
1212
using JetBrains.Annotations;
1313
using System.Linq.Dynamic.Core.Parser;
14+
using System.Text;
15+
1416
#if WINDOWS_APP
1517
using System;
1618
using System.Linq;
@@ -28,7 +30,6 @@ public static class DynamicQueryableExtensions
2830
#if !(WINDOWS_APP45x || SILVERLIGHT)
2931
private static readonly TraceSource TraceSource = new TraceSource(typeof(DynamicQueryableExtensions).Name);
3032
#endif
31-
private static readonly Func<MethodInfo, bool> PredicateParameterHas2 = mi => mi.GetParameters()[1].ToString().Contains("Func`2");
3233

3334
private static Expression OptimizeExpression(Expression expression)
3435
{
@@ -1451,7 +1452,15 @@ public static IQueryable Skip([NotNull] this IQueryable source, int count)
14511452
#endregion Skip
14521453

14531454
#region SkipWhile
1454-
private static readonly MethodInfo _skipWhilePredicate = GetMethod(nameof(Queryable.SkipWhile), 1, PredicateParameterHas2);
1455+
1456+
private static readonly MethodInfo _skipWhilePredicate = GetMethod(nameof(Queryable.SkipWhile), 1, mi =>
1457+
{
1458+
return mi.GetParameters().Length == 2 &&
1459+
mi.GetParameters()[1].ParameterType.GetTypeInfo().IsGenericType &&
1460+
mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>) &&
1461+
mi.GetParameters()[1].ParameterType.GetGenericArguments()[0].GetTypeInfo().IsGenericType &&
1462+
mi.GetParameters()[1].ParameterType.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(Func<,>);
1463+
});
14551464

14561465
/// <summary>
14571466
/// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
@@ -1512,7 +1521,15 @@ public static IQueryable Take([NotNull] this IQueryable source, int count)
15121521
#endregion Take
15131522

15141523
#region TakeWhile
1515-
private static readonly MethodInfo _takeWhilePredicate = GetMethod(nameof(Queryable.TakeWhile), 1, PredicateParameterHas2);
1524+
1525+
private static readonly MethodInfo _takeWhilePredicate = GetMethod(nameof(Queryable.TakeWhile), 1, mi =>
1526+
{
1527+
return mi.GetParameters().Length == 2 &&
1528+
mi.GetParameters()[1].ParameterType.GetTypeInfo().IsGenericType &&
1529+
mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>) &&
1530+
mi.GetParameters()[1].ParameterType.GetGenericArguments()[0].GetTypeInfo().IsGenericType &&
1531+
mi.GetParameters()[1].ParameterType.GetGenericArguments()[0].GetGenericTypeDefinition() == typeof(Func<,>);
1532+
});
15161533

15171534
/// <summary>
15181535
/// Returns elements from a sequence as long as a specified condition is true.
@@ -1793,11 +1810,18 @@ private static TResult Execute<TResult>(MethodInfo operatorMethodInfo, IQueryabl
17931810
return source.Provider.Execute<TResult>(optimized);
17941811
}
17951812

1796-
private static MethodInfo GetMethod<TResult>(string name, int parameterCount = 0, Func<MethodInfo, bool> predicate = null) =>
1797-
GetMethod(name, parameterCount, mi => mi.ReturnType == typeof(TResult) && (predicate == null || predicate(mi)));
1798-
1799-
private static MethodInfo GetMethod(string name, int parameterCount = 0, Func<MethodInfo, bool> predicate = null) =>
1800-
typeof(Queryable).GetTypeInfo().GetDeclaredMethods(name).Single(mi => mi.GetParameters().Length == parameterCount + 1 && (predicate == null || predicate(mi)));
1813+
private static MethodInfo GetMethod(string name, int parameterCount = 0, Func<MethodInfo, bool> predicate = null)
1814+
{
1815+
try
1816+
{
1817+
return typeof(Queryable).GetTypeInfo().GetDeclaredMethods(name).Single(mi =>
1818+
mi.GetParameters().Length == parameterCount + 1 && (predicate == null || predicate(mi)));
1819+
}
1820+
catch (Exception ex)
1821+
{
1822+
throw new Exception("Method not found: " + name, ex);
1823+
}
1824+
}
18011825
#endregion Private Helpers
18021826
}
18031827
}

0 commit comments

Comments
 (0)