A co-worker of mine asked me last week if it were possible in C# to extend a generic class from its generic parameter. He said it was possible in C++. What he wanted makes actually sense. He wanted a generic decorator to annotate an arbitrary class with additional information. Something like:
pubic class Decorator<T> : T
{
public object AdditionalInformation {get:set;}
}
So that he can now use this generic decorator everywhere instead of T.
The most similar thing I could come with was a container class with the original object, the additional information and an implicit conversion.
public class Decorator<T>
{
private readonly T _instance;
public Decorator(T instance)
{
_instance = instance;
}
public T Instance
{
get { return _instance; }
}
public object AdditionalInformation { get; set; }
public static implicit operator T(Decorator<T> deco)
{
return deco._instance;
}
}
But this is not the same because the implicit conversion is only one way. He cannot use it, for example, as a return type of a method because the additional information would be lost after he implicit conversion.
Does anybody have a better idea?