且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果测试对象是在C#字典

更新时间:2021-12-19 22:35:56

这应该是像下面这样。我写这个的答案框,以便语法可能不完全正确的,但我做了它的wiki编辑的所以任何人都可以解决了。

It should be something like the following. I wrote this in the answer box so the syntax may not be exactly right, but I've made it Wiki editable so anybody can fix up.

if (listBox.ItemsSource.IsGenericType && 
    typeof(IDictionary<,>).IsAssignableFrom(listBox.ItemsSource.GetGenericTypeDefinition()))
{
    var method = typeof(KeyValuePair<,>).GetProperty("Value").GetGetMethod();
    var item = method.Invoke(listBox.SelectedItem, null);
}