日期:2014-05-19  浏览次数:20999 次

在线求助:泛型问题。
我已经通过反射得到类型实例。

Type   DllType   =   MyAssembly.GetType( "MyClass ");
Object   DllObj   =   Activator.CreateInstance(DllType   ,   null);
Type   OtherType   =   MyAssembly.GetType( "OtherClass ");
MethodInfo   DllMethod=   DllType.GetMethod( "DllMethod ",   new   Type[]   {   OtherType   });
Object   objs   =   (Object)DllMethod.Invoke(DllObj,   new   Object[]   {   "AllObjects "});
这个返回的objs是一个ReadOnlyCollection <OtherClass> 的集合,但是我怎么把objs这个Object类型变为ReadOnlyCollection <OtherClass> 类型呢?
我试图用ReadOnlyCollection <Object>   collect   =   ReadOnlyCollection <Object>   objs强行转换,但不行。

------解决方案--------------------
你的DllMethod是什么?怎么写的?返回类型是什么?
------解决方案--------------------
拜托,谁说泛型可以这样转啊……

你不是转换成ReadOnlyCollection <OtherClass> 出错么?到底是转换成什么出错!!

我看您考虑先锻炼下表达能力。
------解决方案--------------------
ReadOnlyCollection.Count?
有这个属性吗?有的话,FOR循环,没有的话,当我接分
------解决方案--------------------
//但是这个ReadOnlyCollection <OtherClass> 的是被封装成Object
这句话没看明白...

------解决方案--------------------
转成IList
------解决方案--------------------
IList objs = (IList)DllMethod.Invoke(null, new Object[] { "AllObjects "});