I am making a program that processes thousands of files. For each file I create a FileInfo instance, but I am missing some methods and properties that I need.
I wanted to make my own custom FileInfo class by inherting from FileInfo, but that class is sealed so I can't.
I considered making extension methods for FileInfo, but that seems ugly and it requires me to run the same code multiple times while processing. So instead I came up with a custom class that 'wraps' the FileInfo class.
Here's a part of it:
class MyFileInfo
{
FileInfo _fileInfo;
// wrapper of existing property
public string Name { get { return _fileInfo.Name; } }
// custom property
public string NameWithoutExtension { get; private set; }
// custom property
public string Increment { get; private set; }
public MyFileInfo(string filePath)
{
_fileInfo = new FileInfo(filePath);
NameWithoutExtension = GetNameWithoutExtension();
Increment = GetIncrement();
}
private string GetNameWithoutExtension()
{
return _fileInfo.Name.Replace(_fileInfo.Extension, string.Empty);
}
private string GetIncrement()
{
return Regex.Match(NameWithoutExtension, @" #?\d{1,4}$").Value;
}
}
Now my question is: is this the best way to do this? How else could one work around not being able to inherit a sealed class?