0 .net c# generics reflection list
好吧,我有一个类,我传递一个对象作为属性.
我传递的对象是 List<X>
在我的班级我试图通过反射访问对象索引,但我不能!
例:
这节课的作品我写下了我想给你看的部分,我需要帮助.
class MyClass
{
private object _recordSet;
public object RecordSet
{
get { return _recordSet; }
set { _recordSet = value; }
}
public string Draw()
{
system.reflection.Assembly asem = system.reflection.Assembly.getAssembly(_dataSource.GetType());
object instance;
instance = asem.CreateInstance(_dataSource.GetType().UnderlyingSystemType.FullName);
//to access de Count of my List
int recordcount = int.Parse(_dataSource.GetType().GetProperty("Count").GetValue(_dataSource,null));
//i need to do a
for(int cont = 0; cont < recordCount; cont++)
{
_dataSource[cont].Name; // <-- THIS PART IS NOT WORKING!!! because i cant access the Index Directly.... WHAT TO DO!! ???
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果你正在使用反射(因此很多object),为什么不直接转换为IList(非泛型)呢?
即
IList list = (IList)actualList;
object foo = list[17];
Run Code Online (Sandbox Code Playgroud)
另外 - 对于你的原始代码Count,你并不意味着int.Parse- 你应该只是演员(因为我们希望Count是一个int).
| 归档时间: |
|
| 查看次数: |
2966 次 |
| 最近记录: |