Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the following error:
Argument '1': cannot convert from 'GenericsPOC.ArticleCollection' to 'System.Collections.Generic.IList<object>'
I tried casting specifically to IList<object> but then I get a runtime error:
Unable to cast object of type 'GenericsPOC.ArticleCollection' to type 'System.Collections.Generic.IList`1'.
class Program
{
static void Main(string[] args)
{
ArticleCollection ac = new ArticleCollection();
Mapper.MapObject(ac);
Mapper.MapObject((IList<object>)ac); //Another attempt
}
}
public class Article
{
}
public class ArticleCollection : List<Article>
{
}
public class Mapper
{
public static object MapObject(IList<object> list)
{
return null;
}
public static IList<object> MapObject(List<object> list)
{
return list;
}
}