Are generic type constraints possible in blazor?
Asked Answered
W

3

21

How can I restrict TModel to be classes only or to be implementing a specific interface?

@typeparam TModel

cannot get the syntax working.

Waynewayolle answered 17/3, 2020 at 0:7 Comment(1)
It looks like there's an open issue for this in GitHub. There's a solution that might work for you in the thread: github.com/dotnet/aspnetcore/issues/8433#issuecomment-545690917Astri
W
17

The solution is to put the type constraint additionally in a partial code behind class. It works!

Waynewayolle answered 21/3, 2020 at 17:59 Comment(2)
Could you please post an example? I did as you described and Blazor says that the partial has has different base classes, i.e. one (from razor) get compiled without the constraint and the second (c# code behind) with the constraint.Fleck
If Blazor says that the partial has a different base class, then most likely the razor file contains at least one error. That could even be a problem in a used sub view/controlWaynewayolle
D
49

From ASP.NET Core 6.0 onwards, you can use the following syntax to specify generic type constraints:

@typeparam TModel where TModel : IModel
Dayna answered 20/8, 2021 at 15:56 Comment(3)
this is not working for me for some reason. .NET 7, Blazor Server, MudBlazor <LangVersion>latest</LangVersion>, <RazorLangVersion>latest</RazorLangVersion>. Anything I could be missing?Masonry
For me it builds but Razor editor throws errors. I also added a code-behind partial class, no help.Trothplight
If I keep the constraints in the code-behind, and keep typeparam TModel (without constraints) in the razor, it compiles without errors.Trothplight
W
17

The solution is to put the type constraint additionally in a partial code behind class. It works!

Waynewayolle answered 21/3, 2020 at 17:59 Comment(2)
Could you please post an example? I did as you described and Blazor says that the partial has has different base classes, i.e. one (from razor) get compiled without the constraint and the second (c# code behind) with the constraint.Fleck
If Blazor says that the partial has a different base class, then most likely the razor file contains at least one error. That could even be a problem in a used sub view/controlWaynewayolle
F
1

Indeed, the answer posted by Sven works, but there is one modification I needed to do - add <RazorLangVersion>3.0</RazorLangVersion> to my .csproj file. After that the project was compiled. So here are my complete files:

  1. Project.cs
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion> <!-- Important -->
    <Nullable>enable</Nullable>
    <ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
  </PropertyGroup>
</Project>
  1. Razor component
@typeparam TViewModel
@inherits PageBase<TViewModel>

<h3>Some text</h3>
  1. Corebehind
public abstract partial class AuthenticatedPageBase<TViewModel>
    where TViewModel : ViewModelBase
{
}

EDIT: Well, after deleting the <RazorLangVersion> tag from the csproj file it still seems to work.

Fleck answered 14/4, 2021 at 13:7 Comment(1)
RazorLangVersion 3 is implicitely used when using net5.0. I guess it was just a coincidence.Waynewayolle

© 2022 - 2024 — McMap. All rights reserved.