查找对象的所有属性和子属性
有时我想知道一个对象是否具有我正在寻找的属性,但有时一个对象有很多属性,可能需要一些时间才能找到它并进行调试。如果我可以编写一个函数来查找字符串中的所有属性及其值,那么我可以将该字符串粘贴到记事本中,并使用记事本具有的查找功能查找我正在查找的值,那就太好了。到目前为止,我有这样的事情:
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 被单击”。我知道我可以通过名称获取它,但如果我可以实现这个调试器功能,那就太好了。看看下图中它会变得多么复杂。
Sometimes I want to know if an object has a property that I am looking for but sometimes an object has a lot of properties and it may take some time to find it debugging it. It will be nice if I could write a function that will find all the properties and its values in a a string then I can paste that string in notepad for instance and look for the value that I am looking for with the find feature that notepad has. So far I have something like this:
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);
}
}
Editing the question I have worked:
so far I have added the following code. I can pass whatever object I want to the following method and I can see all the properties. I am having trouble viewing the values of the properties
![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);
}
Yeah visual studio debugger is great but look how many properties an object can have. I am actually looking for the indexSomething property of a gridViewColumnHeader I don't remember the exact name I just remember I have used it before. I have an event that fires when a column gets clicked and I would like to know the index not the name "column number 2? or 3 was clicked". I know I can get it with the name but it will be nice if I can implement this debugger function. See how complex it can get in the picture below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要包括基本类型在内的所有属性,那么您可以这样做:
或者您可能想要递归打印属性,直到一定级别:
编辑: 调用上述方法:
上面将读取属性物体的深度最多可达 5 级。
搜索必须以某种方式受到限制,否则它将永远循环......想想一个自引用对象。
If you want all properties including of base type then you could do this:
or maybe you want a recursive print of properties, up to a level:
EDIT: Calling the above method:
The above will read properties of up to 5 levels of depth into the objeto.
The search must be limited somehow, otherwise it would loop forever... think of a self-referencing object.