Home > other >  Blazor pass to generic type to base class
Blazor pass to generic type to base class

Time:05-03

I have the base class ListPage as follows

@inherits TPL.Client.Core.Bases.Page 

@code {

[Inject] protected IConfiguration Configuration { get; set; } = null!;

protected override async Task OnInitializedAsync()
{
    await base.OnInitializedAsync();

    //stuff
}

protected override async Task OnParametersSetAsync()
{       
   //stuff
}

}

and then the inheriting class Organizations as follows

@inherits TPL.Client.Core.Bases.ListPage

@layout TPL.Client.Shared.Layouts.ListPage

@page "/app/organizations"

<PageTitle>Organization</PageTitle>

@if (items != null)
{
    //stuff
}


@code {
    List<Domain.Organization.Organization> items;

    
    protected override async Task OnInitializedAsync() => await base.OnInitializedAsync();

    protected override async Task OnParametersSetAsync() 
    {
        base.OnParametersSetAsync();

       //stuff
    }
}

I'd like to be able to move List<Domain.Organization.Organization> items; to the parent class (ListPage), make it List<TItem> items and then pass the generic type from the child class (Organizations).

Is that's something that's possible?

CodePudding user response:

Given a base class razor file such as:

@typeparam T

@code {
    public List<T> list = new();
}

You can subclass this by using @inherits with the type arguments:

@inherits GenericBaseClass<Domain.Organization.Organization>
  • Related