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:
- Is there some concept in .net the supports something like mentioned above?
- What would be the best solution to have some extra functionality in a class that can’t be changed?