.NET 中运行时交换方法实现?
如何在运行时将方法的实现从 return false;
更改为 return true;
? 我无法控制方法的实现,因为它带有第三方库。 任何解决方法都值得赞赏。 再说一次:我既不控制方法本身,也不控制它的调用者。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
MethodRental.SwapMethodBody
方法。
Try the
MethodRental.SwapMethodBody
method.您可以拦截该方法的调用者并返回 true 而不是调用它。 也许您可以发布更多详细信息。
You can intercept the callers of that method and return true instead calling it. Maybe you can post more details.
请提供有关您的问题的更多信息。 如果您只想切换布尔结果,则简单的
bool result = !MyMethod();
就足够了;)Please provide more information about your problem. If you only want to switch the boolean result a simple
bool result = !MyMethod();
will be enought ;)更多信息会有所帮助。 您可以包装库并让其他人调用您的包装器吗? 这也可能是一个很好的 TDD 途径。 有没有可以自己实现的接口? 同样,DI 可以允许您更换实现。
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.
子类化和覆盖。 如果无法子类化,请使用装饰器模式。
Subclass and override. If you can't subclass, use a Decorator pattern.