简体   繁体   中英

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. 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.

For example I have the following list of classes:

  • ClassA
  • ClassB

I want to show all the members of that class in another listbox when a user clicks "ClassB" from the listbox. Is this possible?

The code to generate the classes from the XML file is:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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