Skip to content

Commit 820fb36

Browse files
author
Sébastien Geiser
committed
Try to make dynamic cast (not working)
1 parent c041b10 commit 820fb36

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

+36-32
Original file line numberDiff line numberDiff line change
@@ -4149,30 +4149,30 @@ protected static object ChangeType(object value, Type conversionType)
41494149
return Enum.ToObject(conversionType, value);
41504150
}
41514151

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-
}
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+
//}
41764176

41774177
if (DynamicCast(value, conversionType, out object ret))
41784178
{
@@ -4188,19 +4188,23 @@ protected static bool DynamicCast(object source, Type destType, out object resul
41884188
if (srcType == destType) { result = source; return true; }
41894189
result = null;
41904190

4191-
BindingFlags bf = BindingFlags.Static | BindingFlags.Public;
4192-
MethodInfo castOperator = destType.GetMethods(bf)
4193-
.Union(srcType.GetMethods(bf))
4194-
.Where(mi => mi.Name == "op_Explicit" || mi.Name == "op_Implicit")
4195-
.Where(mi =>
4191+
BindingFlags bindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy;
4192+
MethodInfo castOperator = destType.GetMethods(bindingFlags)
4193+
.Union(srcType.GetMethods(bindingFlags))
4194+
.Where(methodInfo => methodInfo.Name == "op_Explicit" || methodInfo.Name == "op_Implicit")
4195+
.Where(methodInfo =>
41964196
{
4197-
var pars = mi.GetParameters();
4197+
var pars = methodInfo.GetParameters();
41984198
return pars.Length == 1 && pars[0].ParameterType == srcType;
41994199
})
42004200
.Where(mi => mi.ReturnType == destType)
42014201
.FirstOrDefault();
4202-
if (castOperator != null) result = castOperator.Invoke(null, new object[] { source });
4203-
else return false;
4202+
4203+
if (castOperator != null)
4204+
result = castOperator.Invoke(null, new object[] { source });
4205+
else
4206+
return false;
4207+
42044208
return true;
42054209
}
42064210

0 commit comments

Comments
 (0)