0

I have this code:

if (((Classes.ProductGroup)o).ToString().Contains(comboBox.Text)) 
   return true;
else 
   return false;

Now I want not to specify the part Classes.ProductGroup. I want to make it universal.

How can I replace the part Classes.ProductGroup with a Type object?

Type type = typeof(Classes.ProductGroup);

Something similar to this:

if (((type)o).ToString().Contains(comboBox.Text)) 

Is this possible?

Here is the complete method code:

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();
}
8
  • 2
    all objects have ToString(). cast is redundant Commented Mar 29, 2017 at 8:52
  • what is o and please provide what function you want to achieve Commented Mar 29, 2017 at 8:52
  • 2
    Use generics Commented Mar 29, 2017 at 8:52
  • @ASh - unless that class or one of its ancestors has shadowed the method. (But that does raise the question of why someone would think to do so) Commented Mar 29, 2017 at 8:53
  • 1
    @dns_nx, why is it so important to have concrete type? Commented Mar 29, 2017 at 9:30

2 Answers 2

1

string ToString() method is defined in the base Object class. cast to concrete type is redundant.

string filter = comboBox.Text;

itemsViewOriginal.Filter = o => String.IsNullOrEmpty(filter) || 
                                o != null && o.ToString().Contains(filter);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use generics and build a method like

public bool ClassNameContainString<T>(string text) where T : class
{
    var containsString = typeof(T).ToString().Contains(text);

    return containsString;
}

If you want to make this method case insensitive change the logic to

var containsString = typeof(T).ToString().ToLower().Contains(text.ToLower());

3 Comments

My understanding was that the OP wanted to do ((T)o).ToString(). Its not entirely clear but changing from a cast of an object to using a type seems less likely than changing from casting to a fixed object to a variable object.
but doesn't this function misses the object o? and then I'm in the situation I was before...
Chris, you're right. I wanted to cast o as given type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.