2011-05-11 103 views
2

有时我想知道某个对象是否有我正在查找的属性,但有时对象具有很多属性,并且可能需要一些时间才能找到它。这将是很好,如果我可以编写一个函数,将找到所有的属性和它的价值在一个字符串,然后我可以粘贴该字符串记事本为例,并寻找我正在寻找与记事本查找功能的价值。到目前为止,我有这样的事情:查找对象的所有属性和子属性

public void getAllPropertiesAndSubProperties(System.Reflection.PropertyInfo[] properties) 
     { 
      foreach (var a in properties) 
      { 
       //MessageBox.Show(a.ToString()); 
       // do something here to test if property a is the one 
       // I am looking for 
       System.Reflection.PropertyInfo[] temp = a.GetType().GetProperties(); 
       // if property a has properties then call the function again 
       if (temp.Length > 0) getAllPropertiesAndSubProperties(temp); 
      } 
     } 

编辑我曾经工作过的问题:

到目前为止,我已经添加下面的代码。我可以将任何我想要的对象传递给以下方法,并可以看到所有属性。我无法查看属性

![public void stackOverex(dynamic obj) 
{ 
    // this is the string where I am apending all properties 
    string stringProperties = ""; 

    Type t = obj.GetType(); 
    List<PropertyInfo> l = new List<PropertyInfo>(); 
    while (t != typeof(object)) 
    { 
     l.AddRange(t.GetProperties()); 
     t = t.BaseType; 

     var properites = t.GetType().GetProperties(); 
     foreach (var p in properites) 
     { 
      string val = ""; 
      try 
      { 
       val = obj.GetType().GetProperty(p.Name).GetValue(obj, null).ToString(); 
      } 
      catch 
      { 

      } 
      stringProperties += p.Name + " - " + val + "\r\n"; 
     } 

    } 

    MessageBox.Show(stringProperties); 
} 

enter image description here

呀Visual Studio调试器是伟大的,但看一个对象可以有多少属性具有的值。我实际上正在寻找gridViewColumnHeader的indexSomething属性我不记得我以前使用过的确切名称。我有一个事件,当一个列被点击时触发,我想知道索引不是名称“列号2?或3被点击”。我知道我可以得到它的名字,但是如果我可以实现这个调试器功能,它会很好。看看下面的图片有多复杂。

enter image description here

+1

问题是什么? – Justin 2011-05-11 03:12:34

+0

而你的问题是?这可能很容易以无限循环结束。 – 2011-05-11 03:13:08

+1

你有没有使用过Visual Studio调试器? – mellamokb 2011-05-11 03:14:43

回答

6

如果希望所有特性,包括基本类型,那么你可以这样做:

 Type t = typeof(AnyType); 
     List<PropertyInfo> l = new List<PropertyInfo>(); 
     while (t != typeof(object)) 
     { 
      l.AddRange(t.GetProperties()); 
      t = t.BaseType; 
     } 

或者你想要的属性的递归打印,高达级别:

public static void ReadALotOfValues(StringBuilder b, object o, int lvl, int maxLvl) 
    { 
     Type t = o.GetType(); 
     List<PropertyInfo> l = new List<PropertyInfo>(); 
     while (t != typeof(object)) 
     { 
      l.AddRange(t.GetProperties()); 
      t = t.BaseType; 
     } 
     foreach (var item in l) 
     { 
      if (item.CanRead && item.GetIndexParameters().Length == 0) 
      { 
       object child = item.GetValue(o, null); 
       b.AppendFormat("{0}{1} = {2}\n", new string(' ', 4 * lvl), item.Name, child); 
       if (lvl < maxLvl) 
        ReadALotOfValues(b, child, lvl + 1, maxLvl); 

      } 
     } 
    } 

编辑:调用上述方法:

object o = ...some object here...; 
var b = new StringBuilder(); 
ReadALotOfValues(b, o, 0, 5); 
Console.WriteLine(b.ToString()); 

以上将读取最多达到objeto的5个层次的属性。

搜索必须以某种方式受到限制,否则它将永远循环...想象一个自我引用的对象。

+0

我不明白lvl和max Lvl参数?但你的第一篇文章可能是解决方案。我根据您的解决方案编辑了我的问题。 – 2011-05-11 04:22:07

+0

嗨! lvl是当前的深度级别,maxLvl是最大的深度级别。你可以调用这个方法,例如传递lvl = 0和maxLvl = 5,然后它会查找嵌套深度达到5级的属性。 – 2011-05-13 02:43:45