我有这样的代码:使用类型,而不是特定的类
if (((Classes.ProductGroup)o).ToString().Contains(comboBox.Text))
return true;
else
return false;
现在我想不指定部分Classes.ProductGroup
。我想使它具有普遍性。
如何使用Type
对象替换零件Classes.ProductGroup
?
Type type = typeof(Classes.ProductGroup);
事情与此类似:
if (((type)o).ToString().Contains(comboBox.Text))
这可能吗?
下面是完整的方法代码:
private void FilterPGsinComboBox(object obj)
{
if (!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(new FilterPGsinComboBoxDelegate(this.FilterPGsinComboBox),obj);
return;
}
Type type = ((Dictionary<ComboBox, Type>)obj).First().Value;
ComboBox comboBox = ((Dictionary<ComboBox, Type>)obj).First().Key;
comboBox.IsDropDownOpen = true;
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(comboBox.ItemsSource);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(comboBox.Text))
return true;
else
{
if (((Classes.ProductGroup)o).ToString().Contains(comboBox.Text))
return true;
else
return false;
}
});
itemsViewOriginal.Refresh();
}
所有对象都有'ToString()'。 cast是多余的 – ASh
什么是'o'并且请提供你想要实现的功能 –
使用[泛型](https://msdn.microsoft.com/en-us/library/512aeb7t.aspx) – dotctor