【发布时间】:2014-12-22 13:52:53
【问题描述】:
我的代码中有以下场景:
class Document
{}
class Track : Document
{}
class ViewListClickHandler
{
// I can't change that signature, it's provided by a library I use.
protected override void click(object theList, int index)
{
// I here need to cast "theList" to IList<T> where T : Document
}
}
我现在的问题是(如评论中所述):
如何将theList 转换为IList<T> where T : Document 之类的类型?
在这种情况下,困难在于,我可以获得任何课程,在该列表中扩展 Document。对我来说,我得到的也是一样的。我只想将它转换为包含一些文档的列表。
编辑:
对不起,忘了说。 theList 有时是一个对象,与IList<Document> 兼容,有时是IList<Track> 或一般类型为Document 的某些其他子类型。
我需要在单行中做到这一点。我还有另一个例子,它不是 IList,而是另一个泛型类型的类,我不能循环。
【问题讨论】:
-
@defaultlocale: 抱歉...忘记重命名 cmets 中的一些变量和下面的文本。
-
@defaultlocale
an individual article or unit, especially one that is part of a list, collection, or set.:) -
@defaultlocale 感觉像递归。也许我会为你写一个方法。我一天只说一个字。我在不知道答案的情况下阅读了 9 个问题,然后我看到了你的问题并想嘿,我可以用谷歌搜索。