dotnet/aspnetcore

[Blazor] CascadingTypeParameter problem

daniel-p-tech opened this issue · 5 comments

Hi,

I'm trying to refactor my existing components to use the newly introduced CascadingTypeParameter attribute but I'm running into compiler errors.

BlzTableGrid.razor (this is the main grid component)

@attribute [CascadingTypeParameter(nameof(TItem))]
@typeparam TItem

<CascadingValue Value="this" IsFixed="true">
    @ColumnsTemplate
</CascadingValue>
...

BlzTableGrid.razor.cs

public partial class BlzTableGrid<TItem> where TItem : class
{
    [Parameter]
    public RenderFragment ColumnsTemplate { get; set; }
...

BlzGridBaseColumn.cs

public abstract partial class BlzGridBaseColumn<TItem> : ComponentBase where TItem : class
{
    [CascadingParameter]
    internal BlzTableGrid<TItem> Grid { get; set; }
...

BlzGridColumn.cs

public class BlzGridColumn<TItem> : BlzGridBaseColumn<TItem>, IGridFieldColumn<TItem> where TItem : class
{
    [Parameter]
    public string FieldName { get; set; }
...

TableGrid.razor (page consuming the grid component)

<BlzTableGrid
    TItem="WeatherForecastLookup"
    Items="WeatherForecasts"

    <ColumnsTemplate>
        <BlzGridEditColumn />
        <BlzGridDeleteColumn />
        <BlzGridViewColumn />
        <BlzGridColumn Title="Date" FieldName="@nameof(WeatherForecastLookup.Date)" Format="d" Width="10rem" />
...

I'm getting the following compiler error for what seems to be each column in TableGrid.razor page:

>C:\Projects\Blazor-Components\DemoApp\Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Pages_Grid_TableGrid_razor.g.cs(563,29,563,34): error CS0449: The 'class', 'struct', 'unmanaged', 'notnull', and 'default' constraints cannot be combined or duplicated, and must be specified first in the constraints list.

Thank you for your help.

@mkArtakMSFT I see that this issue has been assigned to the .NET 7 milestone. Does it mean that I will have to wait a year until the problem is resolved? I suspect other 3rd party component vendors will want to refactor their libraries to take advantage of the new feature and from what I've seen their grid components are structured in a similar fashion.

Thanks.

@daniel-p-tech same error!

The generated code seems to be incorrect and incorrectly adds a comma, before the class constraint.

internal static class TypeInference
{
    public static void CreateBlzGridColumn_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, TItem __syntheticArg0, int __seq0, System.Object __arg0, int __seq1, global::System.String __arg1, int __seq2, System.Object __arg2, int __seq3, System.Object __arg3)
        where TItem : , class
    {
    __builder.OpenComponent<global::BlazorApp66.BlzGridColumn<TItem>>(seq);
    __builder.AddAttribute(__seq0, "Title", __arg0);
    __builder.AddAttribute(__seq1, "FieldName", __arg1);
    __builder.AddAttribute(__seq2, "Format", __arg2);
    __builder.AddAttribute(__seq3, "Width", __arg3);
    __builder.CloseComponent();
    }
}

Seems like the constraint is not being correctly represented in the intermediate node

image

Seems that this happens when we construct the constraint

image