-1

I have a 'BindingListView View' bound to a 'DataGridView' of Equin.ApplicationFramework. According to the documents filtering is done as:

View.ApplyFilter(
    delegate (SomeViewModel item)
    {
        return item.Code == textBox1.Text;

    }
);

I need a filter method that can filter item.Code == 'SomeText' regardless of type of items in the BindingListView is there any way to achieve this? I've come up with

View.ApplyFilter(
    delegate (object item)
    {
        return item.GetType().GetProperty("Code").GetValue(item).ToString() == textBox1.Text;

    }
);

Unfortunately it does not work. I get compiler error:

cannot convert anonymous method to type 'delegate' because it is not a delegate type

I also tried the solution here no success.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
roozbeh S
  • 1,084
  • 1
  • 9
  • 16
  • use interface ... also how `ApplyFilter` and `View` is defined – Selvin Nov 22 '18 at 23:51
  • Note that the type of the parameter in your delegate needs to be the same as the generic type parameter you did chose for your `BindingListView`. As such, you trying to use `delegate (object item)` looks kinda wrong to me. –  Nov 22 '18 at 23:55

1 Answers1

1

It looks like that method takes a Predicate<T>. Have you tried using predicate syntax?

View.ApplyFilter(i => (string)i.GetType().GetProperty("Code").GetValue(i) == "SomeText")

Alternately, it would be a better design to have all of the classes implement a common interface and use that to access Code. If there's no way around using reflection, you should at least cache the PropertyInfo for each distinct type used as querying type information is pretty slow.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47