File tree 2 files changed +44
-2
lines changed
NHibernate.Test/Criteria/Lambda
2 files changed +44
-2
lines changed Original file line number Diff line number Diff line change @@ -162,6 +162,38 @@ public void GivenNullableIntegerToIntegerCastFails()
162
162
163
163
}
164
164
165
+ [ Test ]
166
+ public void GivenNullableIntegerToInteger ( )
167
+ {
168
+ int ? value = 1 ;
169
+ Expression < Func < int > > expression = ( ) => ( int ) value ;
170
+
171
+ var actual = GetValue ( expression ) ;
172
+
173
+ //Check with expression compile and invoke
174
+ var lambdaExpression = Expression . Lambda ( expression ) . Compile ( ) ;
175
+
176
+ var expected = ( ( dynamic ) lambdaExpression . DynamicInvoke ( ) ) . Invoke ( ) ;
177
+
178
+ Assert . AreEqual ( expected , actual ) ;
179
+ }
180
+
181
+ [ Test ]
182
+ public void GivenIntegerToInteger ( )
183
+ {
184
+ int value = 1 ;
185
+ Expression < Func < int > > expression = ( ) => ( int ) value ;
186
+
187
+ var actual = GetValue ( expression ) ;
188
+
189
+ //Check with expression compile and invoke
190
+ var lambdaExpression = Expression . Lambda ( expression ) . Compile ( ) ;
191
+
192
+ var expected = ( ( dynamic ) lambdaExpression . DynamicInvoke ( ) ) . Invoke ( ) ;
193
+
194
+ Assert . AreEqual ( expected , actual ) ;
195
+ }
196
+
165
197
[ Test ]
166
198
public void GivenIntegerToNullableIntegerCast ( )
167
199
{
Original file line number Diff line number Diff line change @@ -281,10 +281,20 @@ public static object FindValue(Expression expression)
281
281
if ( unaryExpression . Method != null )
282
282
return unaryExpression . Method . Invoke ( null , new [ ] { FindValue ( unaryExpression . Operand ) } ) ;
283
283
284
- if ( unaryExpression . Type == typeof ( object )
285
- || ( Nullable . GetUnderlyingType ( unaryExpression . Type ) ?? unaryExpression . Type ) == unaryExpression . Operand . Type )
284
+ var toType = unaryExpression . Type ;
285
+ var fromType = unaryExpression . Operand . Type ;
286
+ if ( toType == typeof ( object )
287
+ || toType == fromType
288
+ || Nullable . GetUnderlyingType ( toType ) == fromType )
286
289
return FindValue ( unaryExpression . Operand ) ;
287
290
291
+ if ( toType == Nullable . GetUnderlyingType ( fromType ) )
292
+ {
293
+ var val = FindValue ( unaryExpression . Operand ) ;
294
+ if ( val != null )
295
+ return val ;
296
+ }
297
+
288
298
break ;
289
299
}
290
300
You can’t perform that action at this time.
0 commit comments