Conversão de lista genérica para uma classe derivada de uma lista
Eu criei uma classe derivada de List. No entanto, quando tentei converter a classe para outro objeto de lista, recebo um erro de tempo de execução. Meu código é semelhante ao postado abaixo:
public class CategoryObj
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
public class CategoryObjCollection:List<CategoryObj>
{
}
public void Test()
{
CategoryObjCollection cat = new CategoryObjCollection();
using (NWDataContext db = new NWDataContext())
{
var qry = (from c in db.Categories
select new CategoryObj
{
CategoryID = c.CategoryID,
CategoryName = c.CategoryName,
Description = c.Description
}).ToList();
cat = (CategoryObjCollection)qry; //runtime error Unable to cast object of type 'System.Collections.Generic.List`1[WindowsFormsApplication1.CategoryObj]' to type 'WindowsFormsApplication1.CategoryObjCollection'.
}
}
Espero que alguém possa me ajudar nisso. Obrigado.