オブジェクトのすべてのプロパティとプロパティ値を取得しようとしている次のコードがあります。いくつかのプロパティは、コレクションまたはコレクションのコレクションである可能性があるので、これらのタイプの再帰関数を設定しようとしました。残念ながらそれは、ワーキングこのライン再帰的エラーハンドラ
if (property.GetValue(item, null) is IEnumerable)
にerroringと私が変更された必要があるか分からないではないです。誰でもここで助けることができますか?ありがとう。
public static string PackageError(IEnumerable<object> obj)
{
var sb = new StringBuilder();
foreach (object o in obj)
{
sb.Append("<strong>Object Data - " + o.GetType().Name + "</strong>");
sb.Append("<p>");
PropertyInfo[] properties = o.GetType().GetProperties();
foreach (PropertyInfo pi in properties)
{
if (pi.GetValue(o, null) is IEnumerable && !(pi.GetValue(o, null) is string))
sb.Append(GetCollectionPropertyValues((IEnumerable)pi.GetValue(o, null)));
else
sb.Append(pi.Name + ": " + pi.GetValue(o, null) + "<br />");
}
sb.Append("</p>");
}
return sb.ToString();
}
public static string GetCollectionPropertyValues(IEnumerable collectionProperty)
{
var sb = new StringBuilder();
foreach (object item in collectionProperty)
{
PropertyInfo[] properties = item.GetType().GetProperties();
foreach (var property in properties)
{
if (property.GetValue(item, null) is IEnumerable)
sb.Append(GetCollectionPropertyValues((IEnumerable)property.GetValue(item, null)));
else
sb.Append(property.Name + ": " + property.GetValue(item, null) + "<br />");
}
}
return sb.ToString();
}
エラーと言うと、例外が発生していますか?エラーはどういう意味ですか? – Chris
エラーメッセージに「Parameter count mismatch」と表示されます。 –
オブジェクトのソースは何ですか?たとえば、Excelの場合、プロパティがインデックス付きのプロパティである可能性があります。その場合、nullをproperty.GetValue()に渡すことはできません。 – phoog