@@ -96,10 +96,13 @@ interface IEqualitySignatures : IRelationalSignatures
96
96
{
97
97
void F ( bool x , bool y ) ;
98
98
void F ( bool ? x , bool ? y ) ;
99
- void F ( DateTime x , string y ) ;
100
- void F ( DateTime ? x , string y ) ;
101
- void F ( string x , DateTime y ) ;
102
- void F ( string x , DateTime ? y ) ;
99
+
100
+ // Disabled 4 lines below because of : https://github.com/StefH/System.Linq.Dynamic.Core/issues/19
101
+ //void F(DateTime x, string y);
102
+ //void F(DateTime? x, string y);
103
+ //void F(string x, DateTime y);
104
+ //void F(string x, DateTime? y);
105
+
103
106
void F ( Guid x , Guid y ) ;
104
107
void F ( Guid ? x , Guid ? y ) ;
105
108
void F ( Guid x , string y ) ;
@@ -614,8 +617,7 @@ Expression ParseComparison()
614
617
Token op = _token ;
615
618
NextToken ( ) ;
616
619
Expression right = ParseShift ( ) ;
617
- bool isEquality = op . id == TokenId . Equal || op . id == TokenId . DoubleEqual ||
618
- op . id == TokenId . ExclamationEqual || op . id == TokenId . LessGreater ;
620
+ bool isEquality = op . id == TokenId . Equal || op . id == TokenId . DoubleEqual || op . id == TokenId . ExclamationEqual ;
619
621
if ( isEquality && ( ( ! left . Type . GetTypeInfo ( ) . IsValueType && ! right . Type . GetTypeInfo ( ) . IsValueType ) || ( left . Type == typeof ( Guid ) && right . Type == typeof ( Guid ) ) ) )
620
622
{
621
623
if ( left . Type != right . Type )
@@ -1114,6 +1116,7 @@ Expression GenerateConditional(Expression test, Expression expr1, Expression exp
1114
1116
throw ParseError ( errorPos , Res . NeitherTypeConvertsToOther , type1 , type2 ) ;
1115
1117
}
1116
1118
}
1119
+
1117
1120
return Expression . Condition ( test , expr1 , expr2 ) ;
1118
1121
}
1119
1122
@@ -1587,17 +1590,17 @@ static bool IsEnumType(Type type)
1587
1590
1588
1591
void CheckAndPromoteOperand ( Type signatures , string opName , ref Expression expr , int errorPos )
1589
1592
{
1590
- Expression [ ] args = new [ ] { expr } ;
1593
+ Expression [ ] args = { expr } ;
1591
1594
MethodBase method ;
1592
1595
if ( FindMethod ( signatures , "F" , false , args , out method ) != 1 )
1593
- throw ParseError ( errorPos , Res . IncompatibleOperand ,
1594
- opName , GetTypeName ( args [ 0 ] . Type ) ) ;
1596
+ throw ParseError ( errorPos , Res . IncompatibleOperand , opName , GetTypeName ( args [ 0 ] . Type ) ) ;
1595
1597
expr = args [ 0 ] ;
1596
1598
}
1597
1599
1598
1600
void CheckAndPromoteOperands ( Type signatures , string opName , ref Expression left , ref Expression right , int errorPos )
1599
1601
{
1600
- Expression [ ] args = new [ ] { left , right } ;
1602
+ Expression [ ] args = { left , right } ;
1603
+
1601
1604
MethodBase method ;
1602
1605
if ( FindMethod ( signatures , "F" , false , args , out method ) != 1 )
1603
1606
throw IncompatibleOperandsError ( opName , left , right , errorPos ) ;
@@ -1636,6 +1639,22 @@ static MemberInfo FindPropertyOrField(Type type, string memberName, bool staticA
1636
1639
#endif
1637
1640
}
1638
1641
1642
+ /*
1643
+ *
1644
+ BindingFlags flags = BindingFlags.Public | BindingFlags.DeclaredOnly |
1645
+ (staticAccess ? BindingFlags.Static : BindingFlags.Instance);
1646
+ foreach (Type t in SelfAndBaseTypes(type))
1647
+ {
1648
+ MemberInfo[] members = t.FindMembers(MemberTypes.Method,
1649
+ flags, Type.FilterNameIgnoreCase, methodName);
1650
+ int count = FindBestMethod(members.Cast<MethodBase>(), args, out method);
1651
+ if (count != 0) return count;
1652
+ }
1653
+ method = null;
1654
+ return 0;
1655
+
1656
+ */
1657
+
1639
1658
int FindMethod ( Type type , string methodName , bool staticAccess , Expression [ ] args , out MethodBase method )
1640
1659
{
1641
1660
#if ! ( NETFX_CORE || DNXCORE50 || DOTNET5_4 || WINDOWS_APP || DOTNET5_1 || UAP10_0 || NETSTANDARD )
@@ -1651,13 +1670,14 @@ int FindMethod(Type type, string methodName, bool staticAccess, Expression[] arg
1651
1670
method = null ;
1652
1671
return 0 ;
1653
1672
#else
1654
- method = null ;
1655
1673
foreach ( Type t in SelfAndBaseTypes ( type ) )
1656
1674
{
1657
- var methods = t . GetTypeInfo ( ) . DeclaredMethods . Where ( x => ( x . IsStatic || ! staticAccess ) && x . Name . ToLowerInvariant ( ) == methodName . ToLowerInvariant ( ) ) ;
1675
+ MethodInfo [ ] methods = t . GetTypeInfo ( ) . DeclaredMethods . Where ( x => ( x . IsStatic || ! staticAccess ) && x . Name . ToLowerInvariant ( ) == methodName . ToLowerInvariant ( ) ) . ToArray ( ) ;
1658
1676
int count = FindBestMethod ( methods , args , out method ) ;
1659
1677
if ( count != 0 ) return count ;
1660
1678
}
1679
+
1680
+ method = null ;
1661
1681
return 0 ;
1662
1682
#endif
1663
1683
}
@@ -1738,6 +1758,7 @@ int FindBestMethod(IEnumerable<MethodBase> methods, Expression[] args, out Metho
1738
1758
Where ( m => applicable . All ( n => m == n || IsBetterThan ( args , m , n ) ) ) .
1739
1759
ToArray ( ) ;
1740
1760
}
1761
+
1741
1762
if ( applicable . Length == 1 )
1742
1763
{
1743
1764
MethodData md = applicable [ 0 ] ;
@@ -1748,6 +1769,7 @@ int FindBestMethod(IEnumerable<MethodBase> methods, Expression[] args, out Metho
1748
1769
{
1749
1770
method = null ;
1750
1771
}
1772
+
1751
1773
return applicable . Length ;
1752
1774
}
1753
1775
@@ -2195,67 +2217,64 @@ static Expression GenerateGreaterThan(Expression left, Expression right)
2195
2217
{
2196
2218
if ( left . Type == typeof ( string ) )
2197
2219
{
2198
- return Expression . GreaterThan (
2199
- GenerateStaticMethodCall ( "Compare" , left , right ) ,
2200
- Expression . Constant ( 0 )
2201
- ) ;
2220
+ return Expression . GreaterThan ( GenerateStaticMethodCall ( "Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
2202
2221
}
2203
- else if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2222
+
2223
+ if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2204
2224
{
2205
- return Expression . GreaterThan ( left . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( left , Enum . GetUnderlyingType ( left . Type ) ) : left ,
2206
- right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ) ;
2225
+ var leftPart = left . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( left , Enum . GetUnderlyingType ( left . Type ) ) : left ;
2226
+ var rightPart = right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ;
2227
+ return Expression . GreaterThan ( leftPart , rightPart ) ;
2207
2228
}
2229
+
2208
2230
return Expression . GreaterThan ( left , right ) ;
2209
2231
}
2210
2232
2211
2233
static Expression GenerateGreaterThanEqual ( Expression left , Expression right )
2212
2234
{
2213
2235
if ( left . Type == typeof ( string ) )
2214
2236
{
2215
- return Expression . GreaterThanOrEqual (
2216
- GenerateStaticMethodCall ( "Compare" , left , right ) ,
2217
- Expression . Constant ( 0 )
2218
- ) ;
2237
+ return Expression . GreaterThanOrEqual ( GenerateStaticMethodCall ( "Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
2219
2238
}
2220
- else if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2239
+
2240
+ if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2221
2241
{
2222
2242
return Expression . GreaterThanOrEqual ( left . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( left , Enum . GetUnderlyingType ( left . Type ) ) : left ,
2223
2243
right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ) ;
2224
2244
}
2245
+
2225
2246
return Expression . GreaterThanOrEqual ( left , right ) ;
2226
2247
}
2227
2248
2228
2249
static Expression GenerateLessThan ( Expression left , Expression right )
2229
2250
{
2230
2251
if ( left . Type == typeof ( string ) )
2231
2252
{
2232
- return Expression . LessThan (
2233
- GenerateStaticMethodCall ( "Compare" , left , right ) ,
2234
- Expression . Constant ( 0 )
2235
- ) ;
2253
+ return Expression . LessThan ( GenerateStaticMethodCall ( "Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
2236
2254
}
2237
- else if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2255
+
2256
+ if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2238
2257
{
2239
2258
return Expression . LessThan ( left . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( left , Enum . GetUnderlyingType ( left . Type ) ) : left ,
2240
2259
right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ) ;
2241
2260
}
2261
+
2242
2262
return Expression . LessThan ( left , right ) ;
2243
2263
}
2244
2264
2245
2265
static Expression GenerateLessThanEqual ( Expression left , Expression right )
2246
2266
{
2247
2267
if ( left . Type == typeof ( string ) )
2248
2268
{
2249
- return Expression . LessThanOrEqual (
2250
- GenerateStaticMethodCall ( "Compare" , left , right ) ,
2251
- Expression . Constant ( 0 )
2252
- ) ;
2269
+ return Expression . LessThanOrEqual ( GenerateStaticMethodCall ( "Compare" , left , right ) , Expression . Constant ( 0 ) ) ;
2253
2270
}
2254
- else if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2271
+
2272
+ if ( left . Type . GetTypeInfo ( ) . IsEnum || right . Type . GetTypeInfo ( ) . IsEnum )
2255
2273
{
2256
2274
return Expression . LessThanOrEqual ( left . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( left , Enum . GetUnderlyingType ( left . Type ) ) : left ,
2257
2275
right . Type . GetTypeInfo ( ) . IsEnum ? Expression . Convert ( right , Enum . GetUnderlyingType ( right . Type ) ) : right ) ;
2258
2276
}
2277
+
2259
2278
return Expression . LessThanOrEqual ( left , right ) ;
2260
2279
}
2261
2280
@@ -2276,10 +2295,7 @@ static Expression GenerateSubtract(Expression left, Expression right)
2276
2295
static Expression GenerateStringConcat ( Expression left , Expression right )
2277
2296
{
2278
2297
// Allow concat String with something else
2279
- return Expression . Call (
2280
- null ,
2281
- typeof ( string ) . GetMethod ( "Concat" , new [ ] { left . Type , right . Type } ) ,
2282
- new [ ] { left , right } ) ;
2298
+ return Expression . Call ( null , typeof ( string ) . GetMethod ( "Concat" , new [ ] { left . Type , right . Type } ) , new [ ] { left , right } ) ;
2283
2299
}
2284
2300
2285
2301
static MethodInfo GetStaticMethod ( string methodName , Expression left , Expression right )
@@ -2292,10 +2308,9 @@ static Expression GenerateStaticMethodCall(string methodName, Expression left, E
2292
2308
return Expression . Call ( null , GetStaticMethod ( methodName , left , right ) , new [ ] { left , right } ) ;
2293
2309
}
2294
2310
2295
-
2296
2311
static void OptimizeForEqualityIfPossible ( ref Expression left , ref Expression right )
2297
2312
{
2298
- // The goal here is to provide the way to convert some types from the string form in a way that is compatible with Linq-to- Entities.
2313
+ // The goal here is to provide the way to convert some types from the string form in a way that is compatible with Linq to Entities.
2299
2314
//
2300
2315
// The Expression.Call(typeof(Guid).GetMethod("Parse"), right); does the job only for Linq to Object but Linq to Entities.
2301
2316
//
@@ -2304,14 +2319,17 @@ static void OptimizeForEqualityIfPossible(ref Expression left, ref Expression ri
2304
2319
{
2305
2320
right = OptimizeStringForEqualityIfPossible ( ( string ) ( ( ConstantExpression ) right ) . Value , leftType ) ?? right ;
2306
2321
}
2322
+
2307
2323
if ( leftType == typeof ( string ) && left . NodeType == ExpressionType . Constant )
2308
2324
{
2309
2325
left = OptimizeStringForEqualityIfPossible ( ( string ) ( ( ConstantExpression ) left ) . Value , rightType ) ?? left ;
2310
2326
}
2311
2327
}
2328
+
2312
2329
static Expression OptimizeStringForEqualityIfPossible ( string text , Type type )
2313
2330
{
2314
2331
DateTime dateTime ;
2332
+
2315
2333
if ( type == typeof ( DateTime ) &&
2316
2334
DateTime . TryParse ( text , CultureInfo . InvariantCulture , DateTimeStyles . None , out dateTime ) )
2317
2335
return Expression . Constant ( dateTime , typeof ( DateTime ) ) ;
@@ -2681,4 +2699,4 @@ internal static void ResetDynamicLinqTypes()
2681
2699
_keywords = null ;
2682
2700
}
2683
2701
}
2684
- }
2702
+ }
0 commit comments