简体   繁体   中英

C# - How can i wrap a static class

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.

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. Of course, that would mean reimplementing the operations yourself, which may be undesirable...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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