简体   繁体   English

当我想继承和添加属性时如何处理密封类

[英]How to deal with a sealed class when I wanted to inherit and add properties

In a recent question on Stack Overflow , I asked how I might parse through a file name to extra meta info about a file.最近关于 Stack Overflow 的一个问题中,我问我如何通过文件名解析文件的额外元信息。

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 .这个想法是我将保留原始FileInfo对象,同时在实现FileInfo的对象的属性中添加元信息,例如IsMainBackup

However, FileInfo is sealed, which means other classes cannot inherit from it.但是, FileInfo是密封的,这意味着其他类不能从它继承。

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 .我并不十分高兴这个解决方案,因为不是能够使用BackupFileInfo.Length ,我将不得不使用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?有没有更好的方法来处理这个问题?

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.要解决您的属性问题,只需创建一个名为Length的属性,该属性委托给封装的FileInfo对象。

You could add an implicit operator to your class.您可以向类中添加隐式运算符。

Eg:例如:

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

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

You could then treat your BackupFileInfo object like a FileInfo object like so然后你可以像这样对待你的 BackupFileInfo 对象像一个 FileInfo 对象

BackupFileInfo bf = new BackupFileInfo();
...
int mylen = ((FileInfo)bf).Length;

You could just expose the properties on FileInfo you care about.您可以只公开您关心的 FileInfo 上的属性。 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.如果您想将大量属性委托给 FileInfo,这显然变得不那么实用。

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.此外,一个名为FileInfo的道具正在自找麻烦……它可能需要在一些地方消除FileInfo类的歧义。

You can easily wrap the file info properties in your own properties if you like.如果您愿意,您可以轻松地将文件信息属性包装在您自己的属性中。

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

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.这并不能真正解决您的更大问题,但当然您可以让您想要使用的属性充当下面真实属性的代理。 Eg例如

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

(With approriate null-checking of course.) (当然有适当的空检查。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM