在 IronPython 中重新声明 .net 类
谁能告诉我是否可以在 IronPython 中重新声明 C# 类? 如果我有一个 C# 类,我可以从 IronPython 对其进行猴子修补吗?
Can anyone tell me if its possible to redeclare a C# class in IronPython? If I have a C# class, would I be able to monkey-patch it from IronPython?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法从 IronPython 进行猴子修补。 IronPython 对待所有 .NET 类就像 CPython 对待内置类型一样:它们不能被猴子修补。 另一方面,IronRuby 确实支持这一点。
You cannot monkey patch from IronPython. IronPython treats all .NET classes just like CPython treats built-in types: they cannot be monkey patched. IronRuby on the other hand does support this.
您可以从 IronPython 进行猴子补丁,但 IPy 是唯一尊重您的更改的环境; 即,如果您尝试从 IronPython 模拟 File.Create,这对于任何 IPy 代码都可以正常工作,但如果您调用名为 File.Create 的 C# 方法,它将获得真实的方法,而不是模拟的。
You can monkey-patch from IronPython, but IPy is the only environment that will respect your changes; i.e. if you tried to mock out File.Create from IronPython, this would work fine for any IPy code, but if you called a C# method which called File.Create, it would get the real one, not the mock.
为什么要重新声明呢? 将其子类化或者只是创建一个全新的不是更有意义吗?班级?
编辑:请澄清一下,我认为您真正需要的是一个适当的控制反转(IOC)框架,例如 温莎城堡。 基本上,您有一个数据库接口,然后配置 IOC 框架以注入用于生产的真实数据库和用于测试的模拟数据库。
Why would you redeclare it? Wouldn't it make more sense to subclass it or just make a totally new class?
EDIT: Give your clarification, I think what you actually need is a proper inversion of control (IOC) framework, such as Castle Windsor. Basically, you have a database interface, then configure the IOC framework to inject a real database for production, and mock database for testing.