I have a class, which implements this simple interface:
public interface IPosition
{
int? Position { get; set; }
}
And I have a class to resort collection of classes by this Position
property:
public static class ReorderPositions
{
public static IList<IPosition> Reorder(IList<IPosition> list)
{
// different actions, resorting
return positionList;
}
}
So, for testing it I create fake class (other properties are not necessary for me of real class for reordering):
public class ReorderPositionsFakeModel : IPosition
{
public int? Position { get; set; }
}
Then I created a factory class to fill List<ReorderPositionsFakeModel>
:
public static class ReorderPositionFakeModelFactory
{
public static IList<ReorderPositionsFakeModel> Create(List<int> positions)
{
positions = positions.OrderBy(x => x).ToList();
List<ReorderPositionsFakeModel> model = new();
foreach (var position in positions)
{
model.Add(new ReorderPositionsFakeModel { Position = position });
}
return model;
}
}
but when I try to use:
var model = ReorderPositionFakeModelFactory.Create(new List<int> { -2, -1, 0, 4, 5, 6, 7, 9, 10, 11, 12, 18, 19, 20, 21, 23, 24, 25, 26 });
var result = ReorderPositions.Reorder(model);
I got:
Error CS1503 Argument 1: cannot convert from
'System.Collections.Generic.IList<ReorderPositionsFakeModel>'
to'System.Collections.Generic.IList<IPosition>'
why so? What is wrong? ReorderPositionsFakeModel
implements IPosition
, why can't be converted?...