tags:

views:

132

answers:

5

How can i change the implementation of a method at runtime from return false; to return true;? I don't have control over the methods implementation as it comes with a third-party library. Any workaround is appreciated. Again: I do not control neither the method itself nor it's callers.

A: 

A little more info would help. Can you wrap the library and have others call your wrapper? This might be a great TDD avenue too. Are there interfaces involved you can implement yourself? Again, DI could allow you to swap out implementations.

n8wrl
it is a static method.
usr
+2  A: 

Try the MethodRental.SwapMethodBody method.

SLaks
how is this method used? du i have to generate binary il myself?
usr
@Slaks - According to the page you linked it works only for dynamic modules.
Giorgi
@Giorgi: You're right; I didn't notice that.
SLaks
A: 

You can intercept the callers of that method and return true instead calling it. Maybe you can post more details.

No, he can't. Read the question
SLaks
SLaks, I ment using reflection he could intercept the callers of that method. I have read the question.
How ?
SLaks
A: 

Please provide more information about your problem. If you only want to switch the boolean result a simple bool result = !MyMethod(); will be enought ;)

Alexander
A: 

Subclass and override. If you can't subclass, use a Decorator pattern.

He can't do either - it's a static method (see first comment to first answer) and he doesn't control the call sites
SLaks