void when_the_hooked_function_is_called() { act = () => _result = _subject.Managed(2.0); context["and the hook was applied"] = () => { before = () => { _hook.Apply(); GC.Collect(); }; it["should call the hook function"] = () => _result.should_be(1); it["should be applied"] = () => _hook.IsApplied.should_be_true(); context["and then removed again"] = () => { before = () => _hook.Remove(); it["should call the original function"] = () => _result.should_be(2); it["should not be applied"] = () => _hook.IsApplied.should_be_false(); }; }; after = () => _hook.Dispose(); }
public object Invoke(params object[] parameters) { object ret; bool wasApplied = _hook.IsApplied; try { if (wasApplied) { _hook.Remove(); } ret = _targetDelegate.DynamicInvoke(parameters); } finally { if (wasApplied) { _hook.Apply(); } } return(ret); }