Exception in Reflection and System.Collections.Generic.List

D

DirkTheDaring

Hi,


i have some generic List instances which where created for
different class (e.g. List<int> , or List<string>).

Now i have a reflection framework in place which tries to get all the
objects of the lists.

This is what i would do in a "standard" Code
for (int i=0; i< List.Count; i++) {
object o = List
}

But i have to use reflection.
I have retrieved the "Count" Property via reflection and now i try to
get the object
via the indexProperty.

Type[] typeList = new Type[] { typeof(int) };
PropertyInfo propertyInfo2 = valueType.GetProperty("Item", typeList);

now i try
for (int i=0; i < count ; i++) {

object[] index = new object[1];
index[0] = (int)i;
object val2 = propertyInfo.GetValue(value,index);
}

I always get a ´TargetParameterCountException

System.Reflection.TargetParameterCountException was unhandled
Message="Parameter count mismatch."
Source="mscorlib"
StackTrace:
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo
culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj,
Object[] index)



What is wrong with that code?


- Dietmar
 
D

DirkTheDaring

Finding my own solution.

Did a type (yes i now variable naming is important), should have used
propertInfo2 in this case.

- Dirk The Daring :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top