My interface IDerived is inherited from IBase:
public IDerived : IBase { ... }
I have method which needs IList:
public MyClass
{
public static void DoSmth(IList<IBase> bases)
{ ... }
}
But attempt to pass list of derived objects:
IList<IDerived> derivedObjs = ...;
MyClass.DoSmth(derivedObjs);
causes error:
Argument type 'System.Collections.Generic.IList<IDerived> is not assignable to parameter type 'System.Collections.Generic.IList<IBase>'
I can implement something 'stupid' like that:
MyClass.DoSmth(derivedObjs.Select(d=>d as IBase).ToList());
But that sounds... unprofessional.
I remember few years ago I was fighting with similar problem and that should be resolved somehow by allowing either 'DoSmth' or 'MyClass' to convert objects to base class... but cannot find any solution.
Please advise, what is the proper way to pass list of derived objects to the method which expects list of objects pointed by base class.
Thanks