tags:

views:

228

answers:

7

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?

+3  A: 

You could just expose the properties on FileInfo you care about. Something like this:

public long Length { get { return FileInfo.Length; } }

This obviously becomes less practical if you want to delegate a lot of properties to FileInfo.

Sean Devlin
`Length` is `long`, not `int`
Marc Gravell
Not really the point, but corrected. Thanks.
Sean Devlin
+8  A: 

This is one of the classic composition instead of inheritance examples and you went in the right direction.

To solve your property problem just create a property called Length that delegates to the encapsulated FileInfo object.

Jason Punyon
Exactly what I was about to write.
wheaties
This is also named `Least Knowledge Principle`
Alex Bagnolini
+2  A: 

Pass-thru?

class BackupFileInfo : IEquatable<BackupFileInfo>
{
    public long Length {get {return FileInfo.Length;}}
    //.... [snip]
}

Also, a prop called FileInfo is asking for trouble... it may need disambiguation against the FileInfo class in a few places.

Marc Gravell
Good point about the `FileInfo` naming convention. I was trying to avoid annoying myself with names like `MyFileInfo`, `ThisFileInfo`, `SuperFileInfo`.
Ben McCormack
Actually, giving a property the same name than its type (when it makes sense) is recommended over adding such prefixes and common enough in the BCL (SolidBrush.Color, for example). See Naming Guidelines at http://msdn.microsoft.com/en-us/library/fzcth91k.aspx .
Trillian
+1  A: 

You can easily wrap the file info properties in your own properties if you like.

public long Length
{
    get
    {
       return this.FileInfo.Length;
    }
}
Paul Creasey
`Length` is `long`, not `int`
Marc Gravell
fixed, it was a guess really!
Paul Creasey
+1  A: 

This doesn't really solve your larger problem, but of course you can just make the properties you want to use act as proxies to the real properties underneath. E.g.

public long Length
{
    get {return FileInfo.Length;}
}

(With approriate null-checking of course.)

Gaz
+2  A: 

You could add an implicit operator to your class.

Eg:

class BackupFileInfo .... {
  /* your exiting code */

  public static implicit FileInfo( BackupFileInfo self ){
     return self.FileInfo;
  }
}

You could then treat your BackupFileInfo object like a FileInfo object like so

BackupFileInfo bf = new BackupFileInfo();
...
int mylen = ((FileInfo)bf).Length;
IanNorton
so I would have to cast the `BackupFileInfo` as a `FileInfo` in order to access the properties and methods of `FileInfo`? If that's the case, I think I'd rather just use `BackupFileInfo.FileInfo`.
Ben McCormack
A: 

It would be nice if extension methods allowed you to also add properties to sealed classes.

clichekiller