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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2023769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:47:12  来源:igfitidea点击:

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

c#.net

提问by Ben McCormack

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 FileInfoobject while adding meta information in the properties of the object that implements FileInfo, such as IsMainBackup.

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

However, FileInfois 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. Perhaps this is the best practice already, but something doesn't feel right.

我对这个解决方案并不感到非常兴奋,因为BackupFileInfo.Length我将不得不使用BackupFileInfo.FileInfo.Length. 也许这已经是最佳实践了,但感觉有些不对劲。

Is there a better way to deal with this problem?

有没有更好的方法来处理这个问题?

采纳答案by Jason Punyon

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 Lengththat delegates to the encapsulated FileInfoobject.

要解决您的属性问题,只需创建一个称为Length委托给封装FileInfo对象的属性。

回答by Sean Devlin

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

您可以只公开您关心的 FileInfo 上的属性。像这样的东西:

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

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

如果您想将大量属性委托给 FileInfo,这显然变得不那么实用。

回答by Marc Gravell

Pass-thru?

通关?

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

Also, a prop called FileInfois asking for trouble... it may need disambiguation against the FileInfoclass in a few places.

此外,一个名为的道具FileInfo正在自找麻烦……它可能需要FileInfo在一些地方消除对类的歧义。

回答by Paul Creasey

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

如果您愿意,您可以轻松地将文件信息属性包装在您自己的属性中。

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

回答by Gaz

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.)

(当然有适当的空检查。)

回答by IanNorton

You could add an implicitoperator 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;