I would like to know if there is way to store in a Dictionary/List/...
of a generic type.
Let's imagine this class:
public class Registry{
private Dictionary<String, MyGenericType<IContainableObject>> m_elementDictionary = new Dictionary<String, MyGenericType<IContainableObject>>();
public void Register<T>(MyGenericType<T> objectToRegister)
where T: IContainableObject
{
m_elementDictionary[objectToRegister.key] = objectToRegister; //This doesn't work
}
}
I don't understand why we can't add this element to the Dictionary
since we know that the argument we receive with a generic type is in fact a MyGenericType<IContainableObject>
due to the where condition.
Please note:
- I know that I can put an interface on
MyGenericType<IContainableObject>
a store a dictionary of this. This is the subject. - I know that I can have an
MyGenericType<IContainableObject>
argument, this is the point either.
I was more looking if the covariance/contravariance can help here?