1
我正在生成一個屬性樹的類,但我有一些包含數組的數組和基本數據類型的問題。參數計數異常:System.String的GetValue
在示例字符串中有屬性字符和長度 如何在不使用長度的情況下使用GetValue訪問字符? 主要的一點,爲什麼我不希望使用length屬性是的,因爲我不知道是否有包含length屬性或不
public class Util
{
public static IDictionary<String,Object> PrintProperties(Type type, Object obj)
{
PropertyInfo[] properties = type.GetProperties();
IDictionary<String, Object> propertyDict = new Dictionary<String,Object>();
if (properties.Length > 1)
{
for (int i = 0; i < properties.Length; i++)
{
Console.WriteLine(properties[i].Name);
if (properties[i].PropertyType.GetProperties().Length > 1)
{
Object value = obj.GetType().GetProperty(properties[i].Name).GetValue(obj, null);
if (value == null)
{
propertyDict[properties[i].Name] = null;
}
else
{
propertyDict[properties[i].Name] = Util.PrintProperties(properties[i].PropertyType, obj.GetType().GetProperty(properties[i].Name).GetValue(obj, null));
}
}
else
{
try
{
// MY PROBLEM
Console.WriteLine("\t" + properties[i].GetValue(obj, null));
propertyDict[properties[i].Name] = properties[i].GetValue(obj, null);
}
catch (TargetParameterCountException e)
{
// Array
}
catch (Exception e)
{
}
}
}
}
return propertyDict;
}
}
我想我很確定。在任何情況下,總會有一個長度屬性? – Cr3at0rX 2011-05-04 10:34:11
你能否提供一個樣本類,它提供了getchars而不是長度。此外,一切的基本類是對象,所以你總是會得到這些長度 – Pankaj 2011-05-04 10:37:56
你是對的。我沒有考慮到這一點。謝謝 – Cr3at0rX 2011-05-04 10:42:46