I have the following problem:
I am currently writing the c# equivalent of PHP's
Type t = obj.GetType(); // 'obj' is my variable, i want to 'dump'
string s = "";
PropertyInfo properties = t.GetProperties();
for (int i = 0; i < properties.Length; i++)
PropertyInfo property = properties[i];
object value = property.GetValue(obj); // <-- throws exception
if (value is ICollection)
// Code for array parsing - is irrelevant for this problem
s += "Element at " + i + ": " + value.GetType().FullName + " " + value.ToString() + "\n";
This has nothing to do with the type implementing
IEnumerable. Rather it's about the type having an indexer. Indexers are considered properties, but they're special properties that need to be given the parameter of the index when getting their value. If you don't, it errors as you're seeing here.
You can use
GetIndexParameters() and check the count of the returned array to determine if the property is an indexer. This will let you either skip that property (which is what I assume you'll want to do here), or use it to get its values.