Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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.

like image 867
DerpyNerd Avatar asked Apr 22 '15 20:04

DerpyNerd


People also ask

Can a sealed method be inherited?

A method can also be sealed, and in that case, the method cannot be overridden. However, a method can be sealed in the classes in which they have been inherited. If you want to declare a method as sealed, then it has to be declared as virtual in its base class.

Can we override sealed class?

If you create a sealed class, it cannot be derived. If you create a sealed method, it cannot be overridden.

What is the advantage of sealed class?

By using sealed class, we can restrict access to the classes and its members with the help of a sealed keyword and we can also avoid inheriting the defined classes from other classes. In C#, a sealed class is a class that cannot be inherited by another class but it can be instantiated.

What are the benefits of using a sealed class over enum?

In a sealed class, we can simply add multiple custom constructors depending on what we need. Furthermore, we can define multiple functions with different names, parameters, and return types. In an enum class, however, we can't define different functions in each enum constant.


2 Answers

There really is no way to inherit from a sealed class in .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.

like image 55
Joseph Gagliardo Avatar answered Sep 20 '22 06:09

Joseph Gagliardo


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:

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);
    }
}
like image 27
Shahar Shokrani Avatar answered Sep 21 '22 06:09

Shahar Shokrani