[英]C# - How can i wrap a static class
我想為System.Io制作util類(例如File,Directory等)。
由於無法對靜態類進行繼承,我想知道如何將一個正確的方法包裝成System.Io.File。
我會創建三種類型:
包含您希望能夠測試的所有方法的界面,例如
public interface IFileSystem { Stream OpenWrite(string filename); TextReader OpenText(string filename); // etc }
委托給系統實現的實現:
public class FrameworkFileSystem : IFileSystem { public Stream OpenWrite(string filename) { return File.OpenWrite(filename); } // etc }
一個虛假的測試實現:
public class FakeFileSystem : IFileSystem { // Probably all kinds of things to allow in-memory files // to be created etc }
你可能不希望將File
中的所有內容放在那里 - 因為許多操作可以由“核心”操作組成。 當然,這意味着你自己重新實現這些操作,這可能是不可取的......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.