Skip to content

Commit c5caca5

Browse files
author
Sébastien Geiser
committed
Explicit cast for primitives types ok
1 parent 820fb36 commit c5caca5

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

+14-25
Original file line numberDiff line numberDiff line change
@@ -4148,31 +4148,13 @@ protected static object ChangeType(object value, Type conversionType)
41484148
{
41494149
return Enum.ToObject(conversionType, value);
41504150
}
4151-
4152-
//if(conversionType == typeof(int))
4153-
//{
4154-
// return (int)(dynamic)value;
4155-
//}
4156-
//if (conversionType == typeof(uint))
4157-
//{
4158-
// return (uint)(dynamic)value;
4159-
//}
4160-
//if (conversionType == typeof(long))
4161-
//{
4162-
// return (long)(dynamic)value;
4163-
//}
4164-
//if (conversionType == typeof(ulong))
4165-
//{
4166-
// return (ulong)(dynamic)value;
4167-
//}
4168-
//if (conversionType == typeof(short))
4169-
//{
4170-
// return (short)(dynamic)value;
4171-
//}
4172-
//if (conversionType == typeof(ushort))
4173-
//{
4174-
// return (ushort)(dynamic)value;
4175-
//}
4151+
4152+
if (value.GetType().IsPrimitive && conversionType.IsPrimitive)
4153+
{
4154+
return primitiveExplicitCastMethodInfo
4155+
.MakeGenericMethod(conversionType)
4156+
.Invoke(null, new object[] {value});
4157+
}
41764158

41774159
if (DynamicCast(value, conversionType, out object ret))
41784160
{
@@ -4182,6 +4164,13 @@ protected static object ChangeType(object value, Type conversionType)
41824164
return Convert.ChangeType(value, conversionType);
41834165
}
41844166

4167+
protected static MethodInfo primitiveExplicitCastMethodInfo = typeof(ExpressionEvaluator).GetMethod(nameof(PrimitiveExplicitCast), BindingFlags.Static | BindingFlags.NonPublic);
4168+
4169+
protected static object PrimitiveExplicitCast<T>(dynamic value)
4170+
{
4171+
return (T)value;
4172+
}
4173+
41854174
protected static bool DynamicCast(object source, Type destType, out object result)
41864175
{
41874176
Type srcType = source.GetType();

0 commit comments

Comments
 (0)