Mert
Mert

Reputation: 6572

extension method with type specific List

enter image description here

I want to create an extention method to bind a List, but getting this error.

menuItem.Children.Bind();

public static class Extensions
{
    public static void Bind(this IList list)
    {
        //some stuff
    }
}

class MenuItemMap : Mapper<MenuItem>
{
    public MenuItemMap()
    {
        Id(x => x.MenuItemId);
        Map(x => x.Text);
        HasMany(x => x.Children).KeyColumn("ParentId");
        References(x => x.Parent);
    }
}

public class MenuItem : BaseClass<MenuItem>
{
    public virtual int MenuItemId { get; set; }
    public virtual string Text { get; set; }
    public virtual IList<MenuItem> Children { get; set; }
    public virtual MenuItem Parent { get; set; }

    public MenuItem()
    {
        Children = new List<MenuItem>();
    }

}

Upvotes: 0

Views: 128

Answers (1)

Mehrzad Chehraz
Mehrzad Chehraz

Reputation: 5137

Your extension method is written for IList not IList<T> and because IList<T> does not inherit IList, you need to specify type argument in the extension method:

public static class Extensions
{
    public static void Bind<T>(this IList<T> list)
    {
        //some stuff
    }
}

Upvotes: 3

Related Questions