简体   繁体   English

C# - 如何包装静态类

[英]C# - How can i wrap a static class

I want to make util classes for System.Io (such as File, Directory etc). 我想为System.Io制作util类(例如File,Directory等)。

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. 由于无法对静态类进行继承,我想知道如何将一个正确的方法包装成System.Io.File。

I would create three types: 我会创建三种类型:

  • An interface containing all the methods you want to be able to test etc, eg 包含您希望能够测试的所有方法的界面,例如

     public interface IFileSystem { Stream OpenWrite(string filename); TextReader OpenText(string filename); // etc } 
  • An implementation which delegates to the system implementation: 委托给系统实现的实现:

     public class FrameworkFileSystem : IFileSystem { public Stream OpenWrite(string filename) { return File.OpenWrite(filename); } // etc } 
  • A fake implementation for testing: 一个虚假的测试实现:

     public class FakeFileSystem : IFileSystem { // Probably all kinds of things to allow in-memory files // to be created etc } 

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. 你可能不希望将File中的所有内容放在那里 - 因为许多操作可以由“核心”操作组成。 Of course, that would mean reimplementing the operations yourself, which may be undesirable... 当然,这意味着你自己重新实现这些操作,这可能是不可取的......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM