有个有个 chart 对象  知道chart对象有个属性值为 "=Sheet1!$X$5:$Y$8" 
但是就是不知道哪个属性 
求一个方法遍历 chart 的所有属性 如果 属性值为 "=Sheet1!$X$5:$Y$8" 
则把属性名称显示出来

解决方案 »

  1.   

    下一个端点,然后IDE会显示。写一个也可以IEnumerable<PropertyInfo> GetAllProperties(object o)
    {
        foreach (var item in o.GetType().GetProperties())
        {
            var p = item.GetValue(o, null);
            if (p != null) 
                foreach (var item1 in GetAllProperties(p))
                    yield return item1;
            yield return item;
        }
    }
      

  2.   

    1楼只是把大慨的方法贴出来,修改一下就合适用了
    private string GetPropertiesName(object o)
            {
                foreach (var item in o.GetType().GetProperties())
                {
                    var p = item.GetValue(o, null);
                    if (p != null)
                    {
                        if (p.ToString() == "heet1!$X$5:$Y$8")
                            return item.Name;
                    }
                }
                return "";
            }
      

  3.   

    1楼的我IDE 最终显示 无法计算子集
    4楼的和我自己写的差不多,谢谢4楼
    static string GetAllProperties(object o,string val)
            {
                foreach (var type in o.GetType().GetProperties())
                {
                   var value = type.GetValue(o, null);
                    if (Convert.ToString(value)==val)
                    {
                        return Convert.ToString(value);
                    }
                    if (type.GetType().GetProperties().Length>0)
                    {
                        GetAllProperties(type, val);
                    }
                }
                return null;
            }这个方法调用出现如下错误
    未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中。