internal static Action <BenchmarkContext> CreateDelegateWithContext(object target, MethodInfo invocationMethod) { // As the base method DOES accept a BenchmarkContext as a parameter, we test to see if any other method parameters are required. if (invocationMethod.GetParameters().Length == 1) { // If no additional parameters are required, we create a simple Delegate to the method, that already matches the signature // Action<BenchmarkContext> var simpleDelegate = (Action <BenchmarkContext>)invocationMethod.CreateDelegate(typeof(Action <BenchmarkContext>), target); return(simpleDelegate); } // If additional parameters are required, we use a DynamicMethod to build a Delegate capable of handling 1-n parameters, of any type. var paramaterisedDelegate = DynamicMethodDelegateFactory.CreateMethodCaller(invocationMethod); // As we require a known Action signature, we wrap the delegate in one that specifies Action<BenchmarkContext> Action <BenchmarkContext> wrappedParamaterisedDelegate = context => paramaterisedDelegate(target, _testParamaters); return(wrappedParamaterisedDelegate); }
internal static Action <BenchmarkContext> CreateDelegateWithoutContext(object target, MethodInfo invocationMethod) { // As the base method does not accept a BenchmarkContext as a parameter, and a known Action delegate signature is // required for the sake of performance, we test to see if any method parameters are required. if (invocationMethod.GetParameters().Length == 0) { // If no parameters are required, we create a simple Delegate to the method. var simpleDelegate = (Action)invocationMethod.CreateDelegate(typeof(Action), target); // As we require a knownAction signature, we wrap the simple delegate in one that specifies Action<BenchmarkContext> Action <BenchmarkContext> wrappedSimpleDelegate = context => simpleDelegate(); return(wrappedSimpleDelegate); } // If parameters are required, we use a DynamicMethod to build a Delegate capable of handling 1-n parameters, of any type. var paramaterisedDelegate = DynamicMethodDelegateFactory.CreateMethodCaller(invocationMethod); // Again, as we require a known Action signature, we wrap the delegate in one that specifies Action<BenchmarkContext> Action <BenchmarkContext> wrappedParamaterisedDelegate = context => paramaterisedDelegate(target, _testParamaters); return(wrappedParamaterisedDelegate); }