|
1 | 1 | /******************************************************************************************************
|
2 | 2 | Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
|
3 |
| - Version : 1.4.20.0 |
| 3 | + Version : 1.4.21.0 |
4 | 4 | (if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
|
5 | 5 |
|
6 | 6 | Author : Coding Seb
|
@@ -130,6 +130,21 @@ protected enum TryBlockEvaluatedState
|
130 | 130 | { "void", typeof(void) }
|
131 | 131 | };
|
132 | 132 |
|
| 133 | + // Based on https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/y5b434w4(v=vs.110)?redirectedfrom=MSDN |
| 134 | + protected static readonly IDictionary<Type, Type[]> implicitCastDict = new Dictionary<Type, Type[]> |
| 135 | + { |
| 136 | + { typeof(sbyte), new Type[] { typeof(short), typeof(int), typeof(long), typeof(float), typeof(double), typeof(decimal) } }, |
| 137 | + { typeof(byte), new Type[] { typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(decimal) } }, |
| 138 | + { typeof(short), new Type[] { typeof(int), typeof(long), typeof(float), typeof(double), typeof(decimal) } }, |
| 139 | + { typeof(ushort), new Type[] { typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(decimal) } }, |
| 140 | + { typeof(int), new Type[] { typeof(long), typeof(float), typeof(double), typeof(decimal) } }, |
| 141 | + { typeof(uint), new Type[] {typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(decimal) } }, |
| 142 | + { typeof(long), new Type[] { typeof(float), typeof(double), typeof(decimal) } }, |
| 143 | + { typeof(char), new Type[] { typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double), typeof(decimal) } }, |
| 144 | + { typeof(float), new Type[] { typeof(double) } }, |
| 145 | + { typeof(ulong), new Type[] { typeof(float), typeof(double), typeof(decimal) } }, |
| 146 | + }; |
| 147 | + |
133 | 148 | protected static readonly IDictionary<string, Func<string, CultureInfo, object>> numberSuffixToParse = new Dictionary<string, Func<string, CultureInfo, object>>(StringComparer.OrdinalIgnoreCase) // Always Case insensitive, like in C#
|
134 | 149 | {
|
135 | 150 | { "f", (number, culture) => float.Parse(number, NumberStyles.Any, culture) },
|
@@ -3150,14 +3165,15 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string
|
3150 | 3165 | bool parameterValidate(ParameterInfo p) => p.Position >= modifiedArgs.Count
|
3151 | 3166 | || (testForExtention && p.Position == 0)
|
3152 | 3167 | || modifiedArgs[p.Position] == null
|
3153 |
| - || p.ParameterType.IsAssignableFrom(modifiedArgs[p.Position].GetType()) |
| 3168 | + || IsCastable(modifiedArgs[p.Position].GetType(), p.ParameterType) |
3154 | 3169 | || typeof(Delegate).IsAssignableFrom(p.ParameterType)
|
3155 | 3170 | || p.IsDefined(typeof(ParamArrayAttribute))
|
3156 | 3171 | || (p.ParameterType.IsByRef && argsWithKeywords.Any(a => a.Index == p.Position + (testForExtention ? 1 : 0)));
|
3157 | 3172 |
|
3158 | 3173 | bool methodByNameFilter(MethodInfo m) => m.Name.Equals(func, StringComparisonForCasing)
|
3159 |
| - && (m.GetParameters().Length == modifiedArgs.Count || m.GetParameters().Last().IsDefined(typeof(ParamArrayAttribute), false)) |
3160 |
| - && (typeCopy == typeof(Enumerable) || m.GetParameters().All(parameterValidate)); |
| 3174 | + && (m.GetParameters().Length == modifiedArgs.Count |
| 3175 | + || (m.GetParameters().Last().IsDefined(typeof(ParamArrayAttribute), false) |
| 3176 | + && m.GetParameters().All(parameterValidate))); |
3161 | 3177 |
|
3162 | 3178 | List<MethodInfo> methodInfos = type.GetMethods(flag)
|
3163 | 3179 | .Where(methodByNameFilter)
|
@@ -3213,6 +3229,12 @@ bool methodByNameFilter(MethodInfo m) => m.Name.Equals(func, StringComparisonFor
|
3213 | 3229 | return methodInfo;
|
3214 | 3230 | }
|
3215 | 3231 |
|
| 3232 | + protected virtual bool IsCastable(Type fromType, Type toType) |
| 3233 | + { |
| 3234 | + return toType.IsAssignableFrom(fromType) |
| 3235 | + || (implicitCastDict.ContainsKey(fromType) && implicitCastDict[fromType].Contains(toType)); |
| 3236 | + } |
| 3237 | + |
3216 | 3238 | protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable(MethodInfo methodInfoToCast, List<object> modifiedArgs, string genericsTypes, Type[] inferedGenericsTypes)
|
3217 | 3239 | {
|
3218 | 3240 | MethodInfo methodInfo = null;
|
|
0 commit comments