Parameter count Exception: GetValue of System.String

本文关键字:of System String GetValue count Exception Parameter | 更新日期: 2023-09-27 17:54:30

我正在研究一个生成属性树的类,但是我有一些包含数组的数组和基本数据类型的问题。

在示例中string具有属性charars和Length我如何通过使用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;
    }

}

Parameter count Exception: GetValue of System.String

您确定GetValue将在列表中是确定的吗?那么长度也应该在那里