11using System . Collections ;
22using System . Globalization ;
3+ using System . Reflection ;
34using NUnit . Framework ;
45
56namespace NSubstitute . Acceptance . Specs ;
@@ -12,6 +13,7 @@ public interface ISomethingWithGenerics
1213 void SomeAction < TState > ( int level , TState state ) ;
1314 string SomeFunction < TState > ( int level , TState state ) ;
1415 ICollection < TState > SomeFunction < TState > ( TState state ) ;
16+ ICollection < TState > SomeFunctionWithoutGenericMethodParameter < TState > ( int level ) ;
1517 bool SomeFunctionWithOut < TState > ( out IEnumerable < TState > state ) ;
1618 bool SomeFunctionWithRef < TState > ( ref IEnumerable < TState > state ) ;
1719 void SomeActionWithGenericConstraints < TState > ( int level , TState state ) where TState : IEnumerable < int > ;
@@ -177,4 +179,35 @@ public void Returns_works_with_AnyType_for_ref_parameter_with_AnyType_generic_ar
177179
178180 Assert . That ( result , Is . True ) ;
179181 }
182+
183+ [ Test ]
184+ public void Callback_allows_access_to_method_call ( )
185+ {
186+ static ICollection < T > CreateSubstitute < T > ( int count )
187+ {
188+ ICollection < T > substitute = Substitute . For < ICollection < T > > ( ) ;
189+ substitute . Count . Returns ( count ) ;
190+ return substitute ;
191+ }
192+
193+ MethodInfo methodInfo = typeof ( GenericArguments )
194+ . GetMethods ( BindingFlags . Static | BindingFlags . NonPublic )
195+ . Single ( x
196+ => x . Name . Contains ( nameof ( CreateSubstitute ) )
197+ && x . Name . Contains ( nameof ( Callback_allows_access_to_method_call ) ) ) ;
198+
199+ ISomethingWithGenerics something = Substitute . For < ISomethingWithGenerics > ( ) ;
200+ something
201+ . SomeFunctionWithoutGenericMethodParameter < Arg . AnyType > ( Arg . Any < int > ( ) )
202+ . Returns ( x =>
203+ {
204+ Type argumentType = x . GenericArgs ( ) [ 0 ] ;
205+ MethodInfo method = methodInfo . MakeGenericMethod ( argumentType ) ;
206+ return method . Invoke ( null , [ x . Arg < int > ( ) ] ) ;
207+ } ) ;
208+
209+ ICollection < int > result = something . SomeFunctionWithoutGenericMethodParameter < int > ( 7 ) ;
210+
211+ Assert . That ( result . Count , Is . EqualTo ( 7 ) ) ;
212+ }
180213}
0 commit comments