我正在尝试实现FilePathCollection
。它的项目将是简单的文件名(无路径-如“image.jpg”)。一旦通过foreach
循环使用了集合,它应返回通过与baseDirectory
串联创建的完整路径。我怎样才能做到这一点?
public class FilePathCollection : List<string>
{
string baseDirectory;
public FilePathCollection(string baseDirectory)
{
this.baseDirectory = baseDirectory;
}
new public System.Collections.IEnumerator GetEnumerator()
{
foreach (string value in this._items) //this does not work because _list is private
yield return baseDirectory + value;
}
}
最佳答案
new public IEnumerator GetEnumerator()
{
using(IEnumerator ie = base.GetEnumerator())
while (ie.MoveNext()) {
yield return Path.Combine(baseDirectory, ie.Current);
}
}