C# - 如何包装静态类
我想为 System.Io 创建 util 类(例如文件、目录等)。
由于无法对静态类进行继承,我想知道如何包装 System.Io.File 的正确方法。
I want to make util classes for System.Io (such as File, Directory etc).
Since inheritance cannot be done for static classes i want to know how would be a proper way to wrap lets say System.Io.File.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将创建三种类型:
包含您希望能够测试的所有方法等的接口,例如
委托给系统实现的实现:
用于测试的假实现:
您可能不想将
File
中的所有内容放入其中 - 因为许多操作都可以组合从“核心”开始。当然,这意味着您自己重新实施操作,这可能是不可取的......I would create three types:
An interface containing all the methods you want to be able to test etc, e.g.
An implementation which delegates to the system implementation:
A fake implementation for testing:
You may well not want to put everything from
File
in there though - as many of the operations can be composed from the "core" ones. Of course, that would mean reimplementing the operations yourself, which may be undesirable...