@@ -21,8 +21,8 @@ public static void ContravarianceTest()
2121 [ Fact ]
2222 public static void ChangeDelegateType ( )
2323 {
24- WaitCallback callback = static obj => { } ;
25- callback += static obj => { } ;
24+ WaitCallback callback = static _ => { } ;
25+ callback += static _ => { } ;
2626 var result = callback . ChangeType < SendOrPostCallback > ( ) ;
2727 NotNull ( result ) ;
2828 var list1 = callback . GetInvocationList ( ) . Select ( static d => d . Method ) ;
@@ -603,4 +603,39 @@ public static void HideReturnValue2()
603603
604604 int ChangeValue ( int value ) => box . Value = value ;
605605 }
606+
607+ [ Fact ]
608+ public static void TryInvokeAction ( )
609+ {
610+ static MethodInfo GetMethod ( int argCount )
611+ {
612+ const BindingFlags flags = BindingFlags . Public | BindingFlags . Static | BindingFlags . DeclaredOnly ;
613+ return Single ( typeof ( DelegateHelpers ) . GetMethods ( flags ) ,
614+ candidate => candidate . Name == nameof ( DelegateHelpers . TryInvoke ) && candidate . GetParameters ( ) . Length == argCount + 1 ) ;
615+ }
616+
617+ var successValue = Expression . Empty ( ) ;
618+ var failedValue = Expression . Throw ( Expression . New ( typeof ( ArithmeticException ) ) , typeof ( void ) ) ;
619+ for ( var argCount = 0 ; argCount <= 10 ; argCount ++ )
620+ {
621+ var types = new Type [ argCount ] ;
622+ Array . Fill ( types , typeof ( string ) ) ;
623+ var actionType = Expression . GetActionType ( types ) ;
624+ var parameters = new ParameterExpression [ argCount ] ;
625+ parameters . AsSpan ( ) . ForEach ( static ( ref ParameterExpression p , int _ ) => p = Expression . Parameter ( typeof ( string ) ) ) ;
626+ //prepare args
627+ var args = new object [ parameters . LongLength + 1 ] ;
628+ Array . Fill ( args , string . Empty ) ;
629+ //find method to test
630+ var method = types is [ ] ? GetMethod ( argCount ) : GetMethod ( argCount ) . MakeGenericMethod ( types ) ;
631+ //check success scenario
632+ args [ 0 ] = Expression . Lambda ( actionType , successValue , parameters ) . Compile ( ) ;
633+ var result = ( Exception ) method . Invoke ( null , args ) ;
634+ Null ( result ) ;
635+ //check failure
636+ args [ 0 ] = Expression . Lambda ( actionType , failedValue , parameters ) . Compile ( ) ;
637+ result = ( Exception ) method . Invoke ( null , args ) ;
638+ IsType < ArithmeticException > ( result ) ;
639+ }
640+ }
606641}
0 commit comments