Comment savoir si une propriété est une collection ou un tableau?
Salut,
J'utilise la recursivité pour parcourir un objet. J'aimerai repérer celles qui sont énumérables. Voici la méthode de base
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public static string EntityToString(Object value)
{
if (value != null)
{
StringBuilder sb = new StringBuilder();
Type theType = null;
PropertyInfo propInfo = null;
try
{
theType = value.GetType();
sb.Append(theType.FullName + ":\r\n");
for (int i = 0; i < theType.GetProperties().Length; i++)
{
propInfo = theType.GetProperties()[i];
try
{
sb.Append(string.Format("{0} = {1}\r\n", propInfo.Name, propInfo.GetValue(value, null)));
}
catch
{
}
}
return sb.ToString();
}
catch
{
}
finally
{
propInfo = null;
theType = null;
sb = null;
}
}
else
{
return null;
}
return null;
} |
Auriez-vous une idée?
Merci d'avance
Immo