.NET 中运行时交换方法实现?

发布于 2024-07-27 02:54:41 字数 142 浏览 10 评论 0原文

如何在运行时将方法的实现从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴者 2024-08-03 02:54:41

您可以拦截该方法的调用者并返回 true 而不是调用它。 也许您可以发布更多详细信息。

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

黄昏下泛黄的笔记 2024-08-03 02:54:41

请提供有关您的问题的更多信息。 如果您只想切换布尔结果,则简单的 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 ;)

深白境迁sunset 2024-08-03 02:54:41

更多信息会有所帮助。 您可以包装库并让其他人调用您的包装器吗? 这也可能是一个很好的 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.

念﹏祤嫣 2024-08-03 02:54:41

子类化和覆盖。 如果无法子类化,请使用装饰器模式。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文