Skip to content

Commit 6af5cd9

Browse files
committed
Embedding numeric type min and max values into func as constants instead of using .MinValue and .MaxValue fields
1 parent 610f6b5 commit 6af5cd9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

AgileMapper/TypeConversion/NumericValueIsInRangeComparison.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using Extensions.Internal;
5+
using NetStandardPolyfills;
56
#if NET35
67
using Microsoft.Scripting.Ast;
78
#else
@@ -45,7 +46,7 @@ private static void GetMaximumValueComparisonOperands(
4546
out Expression maxValueComparisonLeftSide,
4647
out Expression maxValueComparisonRightSide)
4748
{
48-
var numericMaxValue = Expression.Field(null, nonNullableTargetType, "MaxValue");
49+
var numericMaxValue = GetValueConstant(nonNullableTargetType, "MaxValue");
4950

5051
if (sourceValue.Type.HasGreaterMaxValueThan(nonNullableTargetType))
5152
{
@@ -66,7 +67,7 @@ private static void GetMinimumValueComparisonOperands(
6667
out Expression minValueComparisonLeftSide,
6768
out Expression minValueComparisonRightSide)
6869
{
69-
var numericMinValue = Expression.Field(null, nonNullableTargetType, "MinValue");
70+
var numericMinValue = GetValueConstant(nonNullableTargetType, "MinValue");
7071

7172
if (sourceValue.Type.HasSmallerMinValueThan(nonNullableTargetType))
7273
{
@@ -79,5 +80,13 @@ private static void GetMinimumValueComparisonOperands(
7980
minValueComparisonRightSide = numericMinValue;
8081
}
8182
}
83+
84+
private static ConstantExpression GetValueConstant(Type nonNullableTargetType, string fieldName)
85+
{
86+
return nonNullableTargetType
87+
.GetPublicStaticField(fieldName)
88+
.GetValue(null)
89+
.ToConstantExpression(nonNullableTargetType);
90+
}
8291
}
8392
}

0 commit comments

Comments
 (0)