Parameter count Exception: GetValue of System.String
I´m working on a clas开发者_高级运维s which generates a property tree, but I have a problem with some arrays and primitive data types which contain arrays.
In example string has properties Chars and Length How can I access Chars by using GetValue without using Length? The main point why I don´t want to use the Length property is, because I don´t know if there is any class which contains a Length property or not
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;
}
}
Are your certain that GetValue will be a sure in the list? Then legth should be there as well.
精彩评论