【发布时间】:2009-04-19 13:18:00
【问题描述】:
虽然我可以将字符串向上转换为对象,但我不能将字符串的 IList 向上转换为对象的 IList。怎么会?除了将所有项目处理到新的 IList 之外,现在还能做什么?
static void ThisWorks()
{
IList<object> list = new List<object>();
list.Add("I can add a string since string : object");
}
static void ThisDoesNotWork()
{
// throws an invalid cast exception
IList<object> list = (IList<object>) new List<string>();
list.Add("I'm never getting here ... why?");
}
【问题讨论】: