从列表元素中获取类型c#

285*_*ser -3 c# listbox gettype

我有一个C#ListBox,里面填充了"Human"类的元素.每个元素都可以是源自Human的类,例如"Old","Young".我想知道ListBox中选择的元素是否为Old.我尝试使用.GetType()但它给了我类型"字符串"因为我是一个列表元素,而我想要类型"旧"或"年轻".

提前致谢

Tim*_*ter 5

你可以使用is-expression:

var query = myListBox.Items.Cast<Human>()
    .Select(h => new { IsOld = h is Old, IsYoung = h is Young, Human = h });
Run Code Online (Sandbox Code Playgroud)

编辑: "我想知道ListBox中选择的元素是否为旧"

那你就不需要LINQ查询了:

Human human = (Human) myListBox.SelectedItem;
bool isOld = human is Old;
Run Code Online (Sandbox Code Playgroud)