有时我想知道某个对象是否有我正在查找的属性,但有时对象具有很多属性,并且可能需要一些时间才能找到它。这将是很好,如果我可以编写一个函数,将找到所有的属性和它的价值在一个字符串,然后我可以粘贴该字符串记事本为例,并寻找我正在寻找与记事本查找功能的价值。到目前为止,我有这样的事情:查找对象的所有属性和子属性
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);
}
呀Visual Studio调试器是伟大的,但看一个对象可以有多少属性具有的值。我实际上正在寻找gridViewColumnHeader的indexSomething属性我不记得我以前使用过的确切名称。我有一个事件,当一个列被点击时触发,我想知道索引不是名称“列号2?或3被点击”。我知道我可以得到它的名字,但是如果我可以实现这个调试器功能,它会很好。看看下面的图片有多复杂。
问题是什么? – Justin 2011-05-11 03:12:34
而你的问题是?这可能很容易以无限循环结束。 – 2011-05-11 03:13:08
你有没有使用过Visual Studio调试器? – mellamokb 2011-05-11 03:14:43