C # - How can I wrap a static class

I want to make usage classes for System.Io (e.g. File, Directory, etc.).

Since inheritance cannot be done for static classes, I want to know how there will be a suitable way to wrap, say System.Io.File.

+6
inheritance c # wrapping
source share
1 answer

I would create three types:

  • An interface containing all the methods you want to test, etc., for example

    public interface IFileSystem { Stream OpenWrite(string filename); TextReader OpenText(string filename); // etc } 
  • The implementation that delegates the implementation of the system:

     public class FrameworkFileSystem : IFileSystem { public Stream OpenWrite(string filename) { return File.OpenWrite(filename); } // etc } 
  • Fake implementation for testing:

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

You may not want to insert everything from File there, because many of the operations can be made up of "core" ones. Of course, this will mean re-executing operations, which may be undesirable ...

+13
source share

All Articles