@@ -130,6 +130,21 @@ protected enum TryBlockEvaluatedState
130130 { "void" , typeof ( void ) }
131131 } ;
132132
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+
133148 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#
134149 {
135150 { "f" , ( number , culture ) => float . Parse ( number , NumberStyles . Any , culture ) } ,
@@ -3150,14 +3165,15 @@ protected virtual MethodInfo GetRealMethod(ref Type type, ref object obj, string
31503165 bool parameterValidate ( ParameterInfo p ) => p . Position >= modifiedArgs . Count
31513166 || ( testForExtention && p . Position == 0 )
31523167 || modifiedArgs [ p . Position ] == null
3153- || p . ParameterType . IsAssignableFrom ( modifiedArgs [ p . Position ] . GetType ( ) )
3168+ || IsCastable ( modifiedArgs [ p . Position ] . GetType ( ) , p . ParameterType )
31543169 || typeof ( Delegate ) . IsAssignableFrom ( p . ParameterType )
31553170 || p . IsDefined ( typeof ( ParamArrayAttribute ) )
31563171 || ( p . ParameterType . IsByRef && argsWithKeywords . Any ( a => a . Index == p . Position + ( testForExtention ? 1 : 0 ) ) ) ;
31573172
31583173 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 ) ) ) ;
31613177
31623178 List < MethodInfo > methodInfos = type . GetMethods ( flag )
31633179 . Where ( methodByNameFilter )
@@ -3213,6 +3229,12 @@ bool methodByNameFilter(MethodInfo m) => m.Name.Equals(func, StringComparisonFor
32133229 return methodInfo ;
32143230 }
32153231
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+
32163238 protected virtual MethodInfo TryToCastMethodParametersToMakeItCallable ( MethodInfo methodInfoToCast , List < object > modifiedArgs , string genericsTypes , Type [ ] inferedGenericsTypes )
32173239 {
32183240 MethodInfo methodInfo = null ;
0 commit comments