In a recent question on Stack Overflow, I asked how I might parse through a file name to extra meta info about a file.
After I worked through that problem, I decided that I might want to create new type of object to hold the meta data and the original file. I thought I might do something like this:
class BackupFileInfo : FileInfo, IEquatable<BackupFileInfo>
{
//Properties and Methods here
}
The idea would be that I would retain the original FileInfo
object while adding meta information in the properties of the object that implements FileInfo
, such as IsMainBackup
.
However, FileInfo
is sealed, which means other classes cannot inherit from it.
Instead, I ended up with the following:
class BackupFileInfo : IEquatable<BackupFileInfo>
{
public bool IsMainBackup { get; set; }
public int ImageNumber { get; set; }
public int IncrementNumber { get; set; }
public FileInfo FileInfo { get; set; }
//public BackupFileInfo() //constructor here
public bool Equals(BackupFileInfo other)
{
return (this.FileInfo.Name == other.FileInfo.Name
&& this.FileInfo.Length == other.FileInfo.Length);
}
}
I'm not terribly excited about this solution because instead of being able to use BackupFileInfo.Length
, I'm going to have to use BackupFileInfo.FileInfo.Length
. Perhaps this is the best practice already, but something doesn't feel right.
Is there a better way to deal with this problem?
question from:
https://stackoverflow.com/questions/2023769/how-to-deal-with-a-sealed-class-when-i-wanted-to-inherit-and-add-properties 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…