How to intercept a call to a nonvirtual method from/to thirdy-party libraries in .Net?

百般思念 提交于 2019-12-04 09:39:19

Possible Solution 1:

Wrap the library and using a tool like ReSharper to find all usages of the library and replace with the wrapper class. You could also use that opportunity to clean up the presumably crappy interface of the third party library.

Possible Solution 2:

While TypeMock is generally used as a testing tool, it will allow you to mock everything. Because it injects itself as a code profiler, the stuff you can mock included private and static members of classes. As a bonus, any overridden methods do not need to be virtual, so you may be able to intercept the calls that way.

My recommendation

I would recommend you go with Solution 1. A wrapper is very easy to understand, and it'll give you a good opportunity to really improve the code. I'd even recommend that you wrap third party libraries as a general rule.

If Bar is static, you might be able to use Moles to detour the method call. Note that this is a pretty heavy-handed approach to fixing the bug and is really not recommended for production code, but it's an option if you're desperate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!