我正在研究一种查找配置文件路径的方法.这需要进行两次传递:首先查找任何现有的配置文件,然后返回并找到第一个可写路径.
虽然我的特殊情况有些过分,但这让我想到:是否可以同时进行懒惰评估并防止多次枚举?
为了说明我的意思,请考虑以下代码:
public IEnumerable<string> GetPaths() { Console.WriteLine("GetPaths() Returning 'one'"); yield return "one"; Console.WriteLine("GetPaths() Returning 'two'"); yield return "two"; Console.WriteLine("GetPaths() Returning 'three'"); yield return "three"; } public bool IsWritable(string path) => false; // testing only
如果我跑:
var paths = GetPaths(); Console.WriteLine("Searching for existing file.."); foreach (var path in paths) { if (File.Exists(path)) { Console.WriteLine($"Found existing file '{path}'"); } } Console.WriteLine("Searching for a writable path.."); foreach (var path in paths.Reverse()) // NOTE: paths enumarated twice { if (IsWritable(path)) { Console.WriteLine($"Found writable path '{path}'"); } } Console.WriteLine("No paths found");
如果文件’one’存在,我们得到:
Searching for existing file.. Returning 'one' Found existing file 'one'
但是,如果没有文件存在,我们得到:
Searching for existing file.. Returning 'one' Returning 'two' Returning 'three' Searching for a writable path.. Returning 'one' Returning 'two' Returning 'three' No paths found
(我们两次浪费地枚举GetPaths()的结果)
var paths = GetPaths().ToList();
Returning 'one' Returning 'two' Returning 'three' Searching for existing file.. Found existing file 'one'
(如我们不必要地枚举列表的其余部分)
换句话说,’one’存在时所需的输出是:
Searching for existing file.. Returning 'one' Found existing file 'one'
如果没有文件:
Searching for existing file.. Returning 'one' Returning 'two' Returning 'three' Searching for a writable path.. No paths found
解决方法
就在这里.如果您的Lazy调用返回的Enumerator从基础数据提供程序的同一实例依次获取其数据,并且该提供程序跟踪它产生的每个项目,那么Enumerator的所有实例实际上都是从数据提供程序的同一实例中提取数据.
考虑一个Enumerator来从文件中返回行;如果该枚举器与该文件的所有其他枚举器共享相同的Stream实例,那么您将获得您所说明的行为.
在伪代码中
static System.IO.FileStream fs; private static void GetFileStream() { if(fs == null) fs = System.IO.File.Open(....); return fs; } public Enumarable<string> GetLines() { // return an enumerator that uses the result of GetFileStream() and // advances the file pointer. All Enumerables returned by this method // will return unique lines from the same file }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。