我有object[]
含有一些值。我想从中提取信息,但是我无法将数组中的第二个对象(一个WeakReference
)投射到一个IList,并将T作为数组中的第三个值。
如何投射对象到IList <T>未知类型T
我的代码看看:
object[] vals = GetValues(); //vals[2] = WeakReference, vals[3] = Type of T, vals[4] = index in the list
IList<((Type)vals[3])> list = (IList<((Type)vals[3])>)(((WeakReference)vals[2]).Target); //This line does not even compile, seems like I'm doing something wrong..
object oo = list.ElementAt((int)vals[4]);
//Do something with oo...
任何建议,我怎么能投出了WeakReference的目标到IList接口与T =瓦尔斯[3]?
为什么不简单地投射到“IList”? – 2011-04-24 12:42:53
,因为我需要'ElementAt'方法,它只能用于IList –
alex
2011-04-24 12:44:45
IList有Item属性,你可以做'list [(int)vals [4]]' – tvanfosson 2011-04-24 12:48:22