I have the following code:
public interface IMenuItem
{
IWebElement Parent { get; }
}
public class MenuItems
{
public T GetMenuItem<T>() where T : IMenuItem, new()
{
var menuItem = new T();
return menuItem;
}
}
public abstract class Page : IEnumerable<IComponent>
{
private Func<IMenuItem> _getMenuItems = new MenuItems().GetMenuItem<IMenuItem>;
}
I'm trying to store the new MenuItems().GetMenuItem<IMenuItem>
function in _getMenuItems
field, but since this is a generic function this isn't working. How can I store generic functions into a variable?
Doing new MenuItems().GetMenuItem<IMenuItem>
won't work and it tells me:
'IMenuItem' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'MenuItems.GetMenuItem()'
Now I have many concrete types that implement IMenuItem
I want the delegate to accept those. I don't want to make a seperate delegate for every concrete type that implements IMenuItem
. Rather I have a single delegate that can accept concrete types that implement IMenuItem
.