In a Blazor .razor file you can use @typeparam MyType
to use generic parameters.
For example:
MyComponent.razo
You were pretty close, just need to add partial
to the class definition:
using Microsoft.AspNetCore.Components;
namespace BlazorApp1.Components
{
public partial class MyCustomComponent<T> : ComponentBase
{
[Parameter]
public string Label { get; set; }
}
}
The Razor part:
@namespace BlazorApp1.Components
@typeparam T
<label>@($"{Label}. Provided type is {typeof(T).Name.ToUpper()}")</label>
The usage (Index.razor):
@page "/"
@using BlazorApp1.Components
<MyCustomComponent T="long" Label="Custom component label" />
This way, you wouldn't need inheriting your component from it, as both become parts of the same class.