11
11
using System . Reflection ;
12
12
using JetBrains . Annotations ;
13
13
using System . Linq . Dynamic . Core . Parser ;
14
+ using System . Text ;
15
+
14
16
#if WINDOWS_APP
15
17
using System ;
16
18
using System . Linq ;
@@ -28,7 +30,6 @@ public static class DynamicQueryableExtensions
28
30
#if ! ( WINDOWS_APP45x || SILVERLIGHT )
29
31
private static readonly TraceSource TraceSource = new TraceSource ( typeof ( DynamicQueryableExtensions ) . Name ) ;
30
32
#endif
31
- private static readonly Func < MethodInfo , bool > PredicateParameterHas2 = mi => mi . GetParameters ( ) [ 1 ] . ToString ( ) . Contains ( "Func`2" ) ;
32
33
33
34
private static Expression OptimizeExpression ( Expression expression )
34
35
{
@@ -1451,7 +1452,15 @@ public static IQueryable Skip([NotNull] this IQueryable source, int count)
1451
1452
#endregion Skip
1452
1453
1453
1454
#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
+ } ) ;
1455
1464
1456
1465
/// <summary>
1457
1466
/// 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)
1512
1521
#endregion Take
1513
1522
1514
1523
#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
+ } ) ;
1516
1533
1517
1534
/// <summary>
1518
1535
/// 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
1793
1810
return source . Provider . Execute < TResult > ( optimized ) ;
1794
1811
}
1795
1812
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
+ }
1801
1825
#endregion Private Helpers
1802
1826
}
1803
1827
}
0 commit comments