I have the following problem:
I am currently writing the c# equivalent of PHP's var_dump
-method. It works perfectly with 'simple' classes and structures (and even with arrays).
My only problem is when it comes to other IEnumerable<T>
, like List<T>
etc:
The debugger is throwing an TargetParameterCountException
. My code looks as follows:
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
}
else
s += "Element at " + i + ": " + value.GetType().FullName + " " + value.ToString() + "\n";
}
return s;
I know, that i can somehow fetch indices with PropertyInfo.GetIndexParameters()
, but I did not figured out, how to use it correctly.
Edit: I also want to note that I neither know the size of the IEnumerable, nor the Type T at compile time.
Aucun commentaire:
Enregistrer un commentaire