I have the following object:
public class ItemChange<T> where T : MyBase
{
public DateTime When { get; set; }
public string Who { get; set; }
public T NewState;
public T OldState;
}
and i am trying to cast and instance of ItemChange<T> to ItemChange<MyBase> but i am getting a cast exception.
Unable to cast object of type . . .
WHat is the proper way to cast a generic type to its base calss (assuming the where constraint above)
You can’t do this directly since classes in C# do not support co-variance. You can do it with interfaces however:
Then you can do:
for some subclass
MySubclassofMyBase.