簡體   English   中英

如何在列表框中顯示在另一個列表框中按名稱選擇的 class 的屬性

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

我有一組來自反序列化 XML 文件的類。 我將這些 class 名稱傳遞到列表框中,但是當我單擊它們時,我需要能夠從這些類中獲取所有屬性。

例如,我有以下類列表:

  • A類
  • B類

當用戶從列表框中單擊“ClassB”時,我想在另一個列表框中顯示該 class 的所有成員。 這可能嗎?

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

你可以試試這個:

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