简体   繁体   English

如何在列表框中显示在另一个列表框中按名称选择的 class 的属性

[英]How to show in a listbox the properties of a class selected by name in another listbox

I have a set of classes that have come from a de-serialized XML file.我有一组来自反序列化 XML 文件的类。 I pass these class names into a listbox but I need to be able to get all the properties off these classes when I click them.我将这些 class 名称传递到列表框中,但是当我单击它们时,我需要能够从这些类中获取所有属性。

For example I have the following list of classes:例如,我有以下类列表:

  • ClassA A类
  • ClassB B类

I want to show all the members of that class in another listbox when a user clicks "ClassB" from the listbox.当用户从列表框中单击“ClassB”时,我想在另一个列表框中显示该 class 的所有成员。 Is this possible?这可能吗?

The code to generate the classes from the XML file is:从 XML 文件生成类的代码是:

var d = Deserialize(@"C:\temp\xml\flat\flat.xml");
            PropertyInfo[] props = d.GetType().GetProperties();
            List<string> propNames = new List<string>();
            foreach (PropertyInfo prp in props)
            {
                listBox1.Items.Add(prp.Name);
            }

You can try this:你可以试试这个:

// Use flags you want
var flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;

// From the listbox containing list of classes names
string name = listBox1.SelectedItem.ToString();

// The listbox where to show properties of the selected class
listBox2.Clear();

foreach ( var property in Type.GetType(name).GetProperties(flags) )
  listBox2.Items.Add(property.Name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM