In a Blazor component, you can create a generic parameter for use in a method just as you can in a typical C# class. To do this, the syntax is:
@typeparam T
But I would like to know how to constrain it as you can in a C# class. Something like
// pseudocode
@typeparam T : ICloneable
For instance, I had the need to create the following component that allows a developer to pass a "Model" of generic type:
.../GESD.Blazor/Shared/GesdTrForm.razor
@typeparam modelType
<EditForm Model="@Model"
OnValidSubmit="@OnValidSubmit"
style="display:table-row"
>
@ChildContent
</EditForm>
@code {
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public modelType Model { get; set; } // here is the use of the generic
[Parameter]
public EventCallback<EditContext> OnValidSubmit { get; set; }
void demo () {
var cloned = Model.Clone();
}
}
But at .Clone()
I get the following error:
'modelType' does not contain a definition for 'Clone' ...