I want to fill a textbox with the count+1 of a kind of figure in a collection. The collection is a Generic List of Figure, figure is an instance of a certain type of Figure.
Generic type parameters need to be specified at compile-time, but GetType()
is a function called at run time, so this simply won't work. The error message indicates that the compiler is trying to interpret your code as figures.OfType < figure.GetType() ...
which doesn't make much sense.
You can do this:
// Count figures whose type is exactly equal to the type of figure
txtName.Text = figures.Count(x => figure.GetType() == x.GetType()).ToString();
// Count figures whose type is equal to or a subtype of the type of figure
txtName.Text = figures.Count(x => figure.GetType().IsAssignableFrom(x.GetType())).ToString();