【发布时间】:2011-04-24 12:38:12
【问题描述】:
我有一个 object[] 包含一些值。我想从中提取信息,但无法将数组中的第二个对象(WeakReference)转换为 IList,其中 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 的目标转换为 T = vals[3] 的 IList 接口吗?
【问题讨论】:
-
你为什么不直接转换成
IList? -
因为我需要
ElementAt方法,它只适用于 IList -
IList有Item属性,可以
list[(int)vals[4]]
标签: c# .net windows generics casting