I have the next classes:
public class EntityBase<T>
{
public T Id { get; set; }
}
And it's implementers:
public class ClassA : EntityBase<Int32>
{
...
}
public class ClassB : EntityBase<Int64>
{
...
}
And in the code, which dont know about classes - ClassA
and ClassB
it knows only about existance of the EntityBase<...>, I do something like this:
// Here for sure I get the list of `ClassA`
object obj = GetSomeHowListOfClassA();
List<EntityBase<Int32>> listOfEntityBases = (List<EntityBase<Int32>>)obj;
And I get the error:
Unable to cast object of type 'System.Collections.Generic.List`1[...ClassA]' to type 'System.Collections.Generic.List`1[...EntityBase`1[System.Int32]]'.
I fix it like this:
var listOfEntityBases = new List<EntityBase<Int32>>(obj);
But I dont like this way, because I'm creating new List<>. Is there way to cast it? Thx for any advance.