能否扩展C#的DirectoryInfo类以适配模拟文件系统?
扩展DirectoryInfo适配基于文本文件的模拟文件系统
我非常喜欢C#提供的DirectoryInfo类,常使用它执行创建、删除、列出文件等操作,示例代码如下:
using System.IO; static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main() { string path = @"D:\MyTestFile1.txt"; DirectoryInfo fl = new DirectoryInfo(path); fl.Create(); Console.WriteLine(di.GetDirectories()); } }
现在我想扩展这个类,让它能够基于文本文件中的目录树(模拟文件系统)工作,实际仅需扩展GetDirectories()和GetFiles()方法。假设提供的文本文件内容如下:
D:\file1.txt D:\file2.py D:\ourFolder\lion.mp4 D:\ourFolder\anotherfolder\imaginara.jpg D:\file3.png D:\file4.jpg
请问是否可以通过某种方式扩展DirectoryInfo类,使其适配该模拟文件系统?
解答
首先要明确:DirectoryInfo是密封类(sealed),无法直接继承扩展,不过可以通过两种方案实现需求:
方案一:创建自定义模拟目录类
实现一个仿DirectoryInfo行为的自定义类,内部解析文本文件中的路径,实现所需方法:
using System.Collections.Generic; using System.IO; using System.Linq; public class MockDirectoryInfo { private readonly List<string> _allPaths; private readonly string _currentPath; // 构造函数:传入当前目录路径和模拟文件系统的文本文件路径 public MockDirectoryInfo(string currentPath, string mockFilePath) { _currentPath = currentPath.TrimEnd(Path.DirectorySeparatorChar); _allPaths = File.ReadAllLines(mockFilePath).ToList(); } // 获取当前目录下的子目录 public MockDirectoryInfo[] GetDirectories() { var subDirs = _allPaths .Where(path => path.StartsWith(_currentPath + Path.DirectorySeparatorChar)) .Select(path => ExtractSubDirectory(path)) .Distinct() .Where(dir => !string.IsNullOrEmpty(dir)) .Select(dir => new MockDirectoryInfo(dir, "")) // 可优化为缓存_allPaths,避免重复读取文件 .ToArray(); return subDirs; } // 获取当前目录下的文件 public FileInfo[] GetFiles() { return _allPaths .Where(path => Path.GetDirectoryName(path) == _currentPath) .Select(path => new FileInfo(path)) .ToArray(); } // 从完整路径中提取当前目录的直接子目录 private string ExtractSubDirectory(string fullPath) { var relativePath = fullPath.Substring(_currentPath.Length + 1); var separatorIndex = relativePath.IndexOf(Path.DirectorySeparatorChar); return separatorIndex > 0 ? $"{_currentPath}{Path.DirectorySeparatorChar}{relativePath.Substring(0, separatorIndex)}" : null; } }
方案二:使用扩展方法
虽然不能继承DirectoryInfo,但可以编写扩展方法,通过额外传入模拟文件路径来实现功能:
using System.IO; using System.Linq; public static class DirectoryInfoExtensions { // 获取模拟文件系统中的子目录 public static DirectoryInfo[] GetMockDirectories(this DirectoryInfo dir, string mockFilePath) { var allPaths = File.ReadAllLines(mockFilePath); var currentDir = dir.FullName.TrimEnd(Path.DirectorySeparatorChar); return allPaths .Where(path => path.StartsWith(currentDir + Path.DirectorySeparatorChar)) .Select(path => Path.GetDirectoryName(path)) .Where(subDir => subDir != currentDir) .Distinct() .Select(subDir => new DirectoryInfo(subDir)) .ToArray(); } // 获取模拟文件系统中的文件 public static FileInfo[] GetMockFiles(this DirectoryInfo dir, string mockFilePath) { var allPaths = File.ReadAllLines(mockFilePath); var currentDir = dir.FullName.TrimEnd(Path.DirectorySeparatorChar); return allPaths .Where(path => Path.GetDirectoryName(path) == currentDir) .Select(path => new FileInfo(path)) .ToArray(); } }
方案对比
- 自定义类方案:完全独立于原
DirectoryInfo,可灵活扩展更多模拟行为,适合大规模依赖模拟文件系统的场景。 - 扩展方法方案:复用原
DirectoryInfo实例,调用方式更贴近原有习惯,但每次调用都需传入模拟文件路径,适合零散的模拟需求。
内容的提问来源于stack exchange,提问作者NinaNuska
相关产品推荐
相关产品推荐

