简体   繁体   English

有继承密封类的替代方法吗?

[英]Is there an alternative to inheriting from sealed classes?

The reason why I'm asking this is because I want to create a class that has all the functionality of the FileInfo class (derives from FileInfo), and allows me to add my own properties to it. 我问这个的原因是因为我想创建一个具有FileInfo类的所有功能的类(派生自FileInfo),并允许我添加自己的属性。

I Think an example will collaborate more. 我认为一个例子会更多地合作。 What I want: 我想要的是:

BindingList<FileInformation> files = new BindingList<FileInformation>();
public void GatherFileInfo(string path)
{
    files.Add(new FileInformation(path));
    listboxFiles.DataContext = files;
}

class FileInformation : FileInfo
{
    public bool selected = false;
}

Versus what I fear I must do: 与我害怕我必须做的事情:

BindingList<FileInformation> files = new BindingList<FileInformation>();
public void GatherFileInfo(string path)
{
    files.Add(new FileInformation(path));
    listboxFiles.DataContext = files;
}

class FileInformation : FileInfo
{
    string path = "<whatever>"
    FileInfo fileInfo = new FileInfo(path);
    public bool selected = false;

    public string Name
    {
        get { return fileInfo.Name }
    }
    //Manually inherit everything I need???
}

The advantage of this would be that in WPF you could simple bind to all the properties of the class FileInformation, including those of the inherited FileInfo class. 这样做的好处是,在WPF中,您可以简单地绑定到FileInformation类的所有属性,包括继承的FileInfo类的属性。

I've never looked into this matter and I have no lead to where I should start looking, so an example or a lead on how to do this would be helpful. 我从来没有调查过这个问题,而且我没有引导到我应该开始寻找的地方,所以一个例子或如何做到这一点的主角将是有帮助的。

There really is no way to inherit from a sealed class in .Net. 真的没有办法从.Net中的密封类继承。 You can write extension methods, but this does not allow you to add new properties or fields. 您可以编写扩展方法,但这不允许您添加新属性或字段。 The only other thing you can do is to simulate inheritance, but making your own class that includes a field of the type of class you want to inherit from, and then manually expose each property and method of the "base" class by writing a wrapper method for each one. 您可以做的唯一其他事情是模拟继承,但是创建自己的类,其中包含您要继承的类类型的字段,然后通过编写包装器手动公开“基类”的每个属性和方法每个人的方法。 It's not bad if the class is small, but if it's a big class it becomes painful. 如果班级很小,那也不错,但如果它是一个大班,那就会变得很痛苦。

I've written code generator programs to use reflection to do this for me automatically. 我已经编写了代码生成器程序来使用反射来自动执行此操作。 Then I take the output of that and extend it. 然后我取出它的输出并扩展它。 But it's not true inheritance. 但这不是真正的继承。 I personally don't like the concept of sealed classes as it prevents extending those classes. 我个人不喜欢密封类的概念,因为它阻止了扩展这些类。 But I suppose they did it for performance reasons. 但我想他们出于性能原因这样做了。

To inherit from sealed class try using the Decorator design pattern, the basic idea is to create private instance of the OldClass, and implement manually all its methods like: 要从密封类继承尝试使用Decorator设计模式,基本思想是创建OldClass的私有实例,并手动实现其所有方法,如:

public class NewClass
{
    private OldClass oldClass = new OldClass();

    public override string ToString() 
    {
        return oldClass.ToString();
    }
    //void example
    public void Method1()
    {
        oldClass.Method1();
    }
    //primitive type example
    public int Method2()
    {
        return oldClass.Method2();
    } 
    //chaining example, please note you must return "this" and (do not return the oldClass instance).
    public NewClass Method3()
    {
        oldClass.Method3();
        return this;
    }
}

public class Demo
{
    static void Main(string[] args)
    {
       var newClass = new NewClass();
       newClass.Method3();
       WriteLine(newClass);
    }
}

As FileInfo inherits from MarshalByRefObject , you can create a custom proxy that mimics a FileInfo and handles all invocations in your own implementations. 由于FileInfo继承自MarshalByRefObject ,您可以创建一个模仿FileInfo的自定义代理,并处理您自己的实现中的所有调用。 However, you wouldn't be able to cast that, and more importantly, you can't extend such class with custom properties. 但是,您无法强制转换,更重要的是,您无法使用自定义属性扩展此类。 Anyway, should someone other want this, SharpUtils have some tools to assist with it. 无论如何,如果其他人想要这个, SharpUtils有一些工具可以帮助它。

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

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