How to access Index of Generic.List By Reflection?
ok, ive the class and i pass the object as a property.
the object I am passing is List<X>
in my class im trying to access the Object index by reflection BUT CANNOT !!!
Example:
this class works, i just wrote down the part i want to show you and i need help.
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!! ???
}
}
}
If you are using reflection (and therefore a lot object
), why not just do IList
(non-generic) instead ?
i.e.
IList list = (IList)actualList;
object foo = list[17];
Also, for your source code with Count
you don't mean int.Parse
- you should just dump (as we expect to Count
be int
).
source to share