Home > other >  'Extension method must be defined in a non-generic static class' in blazor server app razo
'Extension method must be defined in a non-generic static class' in blazor server app razo

Time:10-13

I am using Blazor Server App to create a web application. For the View, razor Pages are used, which can be split up in Front and Backend, so you will always have two files for a page:

ClientPage.razor ClientPage.razor.cs

Since two of my Pages look and work similar, I copied both these files and getting the error on the copied class.

the razor.cs Classes are always "public partial class" and it works on every other page I have.

What could be the issue here?

Code of the class where the error occurs:

public partial class ClientsInUnterwerkPage
{
    [Parameter]
    public string unterwerkid { get; set; }

    public string toAdd;
    public string toRemove;
    public List<Client> notInUw = new List<Client>();
    public List<Client> inUw = new List<Client>();
    public List<Client> allClients = new List<Client>();
    public List<Client> toAddList = new List<Client>();
    public List<Client> toRemoveList = new List<Client>();
    public Unterwerk unterwerk;
    protected override async void OnInitialized()
    {

        using (var repo = new ClientRepository(contextFactory.CreateDbContext()))
        {
            allClients = await repo.GetAll();
            inUw = await repo.GetByUWid(Convert.ToInt32(unterwerkid));

        }

        notInUw = MyExcept2<Client>(allClients, inUw);

        StateHasChanged();
    }

    public void edited()
    {
    }

    public void remove(Client client)
    {
        toRemoveList.Add(client);
        inUw.Remove(client);
    }

    public void returnRemove(Client client)
    {
        inUw.Add(client);
        toRemoveList.Remove(client);
    }

    public void add(Client client)
    {
        toAddList.Add(client);
        notInUw.Remove(client);
    }

    public void returnAdd(Client client)
    {
        notInUw.Add(client);
        toAddList.Remove(client);
    }

    public static List<T> MyExcept2<T>(this List<T> orgList, List<T> toRemove)
    {
        var list = orgList.OrderBy(x => x).ToList();
        foreach (var x in toRemove)
        {
            var inx = list.BinarySearch(x);
            if (inx >= 0) list.RemoveAt(inx);
        }
        return list;
    }
}

CodePudding user response:

You have an extension method in a class that is not declared static. That is not allowed, exactly as the error message states.

While it's not required, I would recommend declaring a class specifically for extension methods on a particular type. In your case, you are extending List<T> so I'd name the class ListExtensions:

public static class ListExtensions
{
    public static List<T> MyExcept2<T>(this List<T> orgList, List<T> toRemove)
    {
        var list = orgList.OrderBy(x => x).ToList();
        foreach (var x in toRemove)
        {
            var inx = list.BinarySearch(x);
            if (inx >= 0) list.RemoveAt(inx);
        }
        return list;
    }
}
  • Related