Aspnetcore: Blazor can not compile generic-typed component with generic array parameter

Created on 15 Oct 2019  路  5Comments  路  Source: dotnet/aspnetcore

Describe the bug

Compiler error when using generic array parameter.

If there is no type parameter:

error RZ10001: The type of component 'Component' cannot be inferred based on the values provided. Consider specifying the type arguments directly using the following attributes: 'TItem'.

If type parameter is specified:

error CS0246: The type or namespace name 'TItem' could not be found (are you missing a using directive or an assembly reference?)

To Reproduce

Component.razor

@typeparam TItem
@code {
    [Parameter]
    public TItem[] List { get; set; }
}

Page.razor

<Component TItem="System.Int32" List="new int[] { 1, 2, 3 }" />

Expected behavior

Compile without errors.

Actual behavior

Compilation error CS0246 in generated file:

__builder.AddAttribute(1, "List", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<TItem[]>());

Workaround

IReadOnlyList<TItem> works fine
affected-very-few area-blazor bug investigate severity-blocking

All 5 comments

Thanks for contacting us, @xiety.
We will look into this post 3.1 milestone. Your workaround looks good for now.

i have a question
can do something like this...

<input @bind="Value"/>

@code{
          [Parameter]
          public object Value {get; set;}
}

@rynowak is this by-design?

Would be great if all those generic issues in Blazor would be fixed soon. It's a sad thing that these bugs will probably remain in .NET 5.0 RTM. Most of the issues are tagged as affected-very-few 馃槥

I have the same issue in .NET 5.

Was this page helpful?
0 / 5 - 0 ratings