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

前端 未结 7 1387
礼貌的吻别
礼貌的吻别 2021-01-02 02:54

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

相关标签:
7条回答
  • 2021-01-02 03:21

    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.

    0 讨论(0)
  • 2021-01-02 03:22

    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 at this 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.

    0 讨论(0)
  • 2021-01-02 03:26

    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.

    0 讨论(0)
  • 2021-01-02 03:30

    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

    0 讨论(0)
  • 2021-01-02 03:31

    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.

    0 讨论(0)
  • 2021-01-02 03:34

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

    0 讨论(0)
提交回复
热议问题