Say I have the follow classes:
public class BaseClass {
...
}
public class ChildClass : BaseClass {
...
}
If I want to pass in single instance of ChildClass as a type BaseClass in a method call, I can do this just fine.
public void SomeCall(BaseClass baseClass)
....
ChildClass childClass = new ChildClass();
SomeCall(childClass); <-- Works fine
However, if I want to pass in a List, I can't seem to cast it correctly.
public void SomeCall(List<BaseClass> baseClasses)
....
List<ChildClass> childClasses = new List<ChildClass>();
SomeCall(childClasses); <-- Gets compiler error
The error I'm getting is
Argument 1: cannot convert from 'System.Collections.Generic.List<ChildClass>'
to 'System.Collections.Generic.List<BaseClass>'
Is there any way to cast the values IN the list to a type of the base class?
Right now, I'm thinking I just have to use a foreach and AutoMap each object in the list to a new list of BaseClass.