views:

127

answers:

6

The problem is that I need a little extra functionality to an object of a class that I can’t change (I’m trying to add data binding support). The best solution that I can think of is to just write a derived class with this functionality. So I can use objects of this class instate. So now the problem is, how do I initialize the objects of the new class? I could make a constructor with the original object as a parameter and initialize the derived object with the values of this object, but to me this seems not to be the smartest solution. It would be nice if I could do something like:

// MyDerivedClass is derived from ObjectOfAnUnchangeableClass.
MyDerivedClass Obj = ObjectOfAnUnchangeableClass as MyDerivedClass;

Of cause this would not work because the ObjectOfAnUnchangeableClass does not know of MyDerivedClass. Another idea would be to have a constructor that could be “initialized” with an object. Something like:

public MyDerivedClass(UnchangeableClass obj): base(obj){}

Here the idea would be that instead of having the base constructor build a new object; it could just take the existing object.

So I have two questions:

  1. Is there some concept in .net the supports something like mentioned above?
  2. What would be the best solution to have some extra functionality in a class that can’t be changed?
+3  A: 

I think Duck Typing could potentially help you. This will basically allow you to "cast" between classes that aren't related with inheritance, but are related in the way their fields look. It looks like they now use a dynamic proxy class to do the translation between the types. I am not exactly sure how this works under the covers, but I am going to find out.

The syntax would be something like this

MyDerivedClass Obj = 
     DuckTyping.Cast<MyDerivedClass>(ObjectOfAnUnchangeableClass);

Essentially it will copy all the fields from ObjectOfAnUnchangeableClass to MyDerivedClass which have the same name. You can archive the same behavior with the method you described, but if you don't like that, give this library a go.

Bob
+6  A: 

You have just about answered this yourself - the 'standard' way to do this is to take an instance of the base class in the constructor of your derived class. It's an example of the decorator pattern

From the wikipedia page

In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing class dynamically.

Steve Willcock
So I guess this will be the path to take. This: http://stackoverflow.com/questions/124336/a-way-of-casting-a-base-type-to-a-derived-type answered me also the first question.
uli78
A: 

Calling the constructor of your derived class and pass the original class as argument is pretty straight forward. If the original class already has a copy constructor, you don't need to copy properties yourself.

Second question, how to add functionality: This depends of what kind of functionality you need to add. Besides of inheritance, there are also extension methods, and quite a few design patterns for special situations.

Stefan Steinegger
A: 
  1. A type convertor perhaps?
  2. You could make an extension method - UnchangeableClass.ToMyDerivedClass();
Richard Hein
+2  A: 

Here is a third option - create a new type that does not extend UnchangeableClass but contains an instance of it that you can use and then expose any new methods from this new type:

class MyDerivedClass
{
    UnchangeableClass unchangeableClass;

    public MyDerivedClass(UnchangeableClass obj)
    {
        this.unchangeableClass = obj;
    }

    // other methods to support whatever you need
}

I believe this is simpler and cleaner than creating a derived type.

Andrew Hare
I agree. I don't know if this will be the best solution for the OP's situation, but it's better to prefer delegation where possible.
ebrown
+3  A: 

Some possible alternatives:

  • Create a wrapper class, without deriving from ObjectOfAnUnchangeableClass, which might not be meant to be derived from
  • Extension methods

P.S: about data binding and extension methods, have you seen this post?
How to call extension methods using Eval in a databound control

Paolo Tedesco
I think extension methods won't work, since I also need properties that support data binding.
uli78