Skip to content

Commit af8212e

Browse files
committed
Fixed compiling of int? -> int due my last refactoring
1 parent ea946ea commit af8212e

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/NHibernate.Test/Criteria/Lambda/ExpressionProcessorFindValueFixture.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,38 @@ public void GivenNullableIntegerToIntegerCastFails()
162162

163163
}
164164

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+
165197
[Test]
166198
public void GivenIntegerToNullableIntegerCast()
167199
{

src/NHibernate/Impl/ExpressionProcessor.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,20 @@ public static object FindValue(Expression expression)
281281
if (unaryExpression.Method != null)
282282
return unaryExpression.Method.Invoke(null, new[] { FindValue(unaryExpression.Operand) });
283283

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)
286289
return FindValue(unaryExpression.Operand);
287290

291+
if (toType == Nullable.GetUnderlyingType(fromType))
292+
{
293+
var val = FindValue(unaryExpression.Operand);
294+
if (val != null)
295+
return val;
296+
}
297+
288298
break;
289299
}
290300

0 commit comments

Comments
 (0)