tags:

views:

593

answers:

6

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

A: 

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

eglasius
+2  A: 

Maybe use the Decorator pattern?

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

LukeH
A: 

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.

Jon Skeet
Thanks for the warning of the dangers and direction (book recommendation).
dotnetdev
Effective Java is an awesome book, even for C# developers. Not all of it is relevant to C#, but it's extremely well written.
Jon Skeet
I would not recommend using TypeMock outside of your test environment.
Brian Rasmussen
+1  A: 

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.

Paul Stovell
+2  A: 

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.

Jonathan C Dickinson
+2  A: 

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.

Neil Barnwell