简体   繁体   中英

How to handle a class you want to extend which is sealed in the .NET library?

I was reading somewhere about how to handle the issue of wanting to extend a sealed class in the .NET Framework library.

This is often a common and useful task to do, so it got me thinking, in this case, what solutions are there? I believe there was a "method" demonstrated to extend a sealed class in the article I read, but I cannot remember now (it wasn't extension methods).

Is there any other way? Thanks

There is 'fake' inheritance. That is, you implement the base class and any interfaces the other class implements:

// Given
sealed class SealedClass : BaseClass, IDoSomething { }

// Create
class MyNewClass : BaseClass, IDoSomething { }

You then have a private member, I usually call it _backing, thus:

class MyNewClass : BaseClass, IDoSomething
{
   SealedClass _backing = new SealedClass();
}

This obviously won't work for methods with signatures such as:

void NoRefactoringPlease(SealedClass parameter) { }

If the class you want to extend inherits from ContextBoundObject at some point, take a look atthis article . The first half is COM, the second.Net. It explains how you can proxy methods.

Other than that, I can't think of anything.

this method may have already been mentioned above by it's formal name, but i don't know it's formal name, so here it is. This example "extends" the TextBox class (example in VB). I believe an advantage of this method is that you do not need to explicitly code or expose built-in members. Hope this is relevant:

VB Class Module "MyTextBox":

public Base as TextBox, CustomProperty as Integer

Private Sub Init(newTextBox as TextBox)
    Set Base = newTextBox
End Sub

public Property Get CustomProperty2() As String
    CustomProperty2 = "Something special"
End Property

To call the code, you might say:

Dim MyBox as New MyTextBox
MyBox.Init MyForm.TextBox3

from here you have access to all built-in members, plus your custom members.

Debug.Print MyBox.Base.Text
MyBox.CustomProperty = 44

For extra polish, you can make Base the default property of the class, and then you can leave out "Base" when you call properties of the Base class. You call Base members like this:

Debug.Print MyBox().Text
MyBox().Text = "Hello World"

VBA Demo

Extension methods is one way, the alternative being the Adapter Pattern. Whereby you write a class that delegates some calls to the sealed one you want to extend, and adds others. It also means that you can adapt the interface completely into something that your app would find more appropriate.

Maybe use the Decorator pattern ?

Other than extension methods, this is the only sensible technique I can think of.

No, you can't extend a sealed class in any legitimate way.

TypeMock allows you to mock sealed classes, but I doubt that they'd encourage you to use the same technique for production code.

If a type has been sealed, that means the class designer has not designed it for inheritance. Using it for inheritance at that point may well cause you lots of pain, either now or when the implementation is changed at a later date.

Prefer composition to inheritance - it's a lot more robust, in my experience. See item 16 in "Effective Java (2nd edition)" for more on this.

The only way I know to "extend" a sealed class without extension methods is by wrapping it. For example:

class SuperString
{
    private String _innerString;

    public SuperString(String innerString)
    {
        _innerString = innerString;
    }

    public int ToInt() 
    {
        return int.Parse(_innerString);
    }
}

You'd need to expose all of the same methods/properties as the string class.

Some frameworks allow you to extend existing objects. In WPF, see Dependency Properties . For Windows Forms, see IExtenderProvider .

How about extension methods? You can "add" additional methods that way, without having to deal with the inheritance restriction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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