Skip to content

Blazor component with generics incorrectly invokes Func<T[]> that is a parameter #34711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
julian-altech opened this issue Jul 26, 2021 · 3 comments · Fixed by #34734
Closed
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved

Comments

@julian-altech
Copy link

julian-altech commented Jul 26, 2021

If a generic component has a parameter property:

@typeparam T

@code {
    [Parameter]
    public Func<T[]> GetItems { get; set; }
}

When you use the component the GetItems will cause a build fail with the error "The type or namespace name T could not be found" since it generates this code for the page:

__builder.OpenComponent<BlazorTest.Client.Shared.GenericComponent<string>>(0);
__builder.AddAttribute(1, "GetItems", new System.Func<T[]>(GetItems)

But there is no definition of T there as T is defined on the comonent, it should generate as string[] not T[]

To Reproduce

With the component above create the following page

@page "/"

<GenericComponent T="string" GetItems="GetItems" />

@code {
    string[] GetItems() => new[]
    {
        "Foo", "Bar"
    };
}

As soon as you try to compile it will fail

I have tested this on 6.0.0-preview.6.21355.2

@javiercn javiercn added the area-blazor Includes: Blazor, Razor Components label Jul 26, 2021
@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us. This should be fixed in the upcoming 6.0-preview7 release.
That was fixed as part of #33357

@campersau
Copy link
Contributor

campersau commented Jul 26, 2021

@mkArtakMSFT I am not sure if they are really the same. This issue is about generic type parameters in combination with arrays which currently aren't supported. See PR #34734

As a workaround use a Func<IList<T>> / Func<List<T>> / ... instead

@julian-altech
Copy link
Author

As a workaround use a Func<IList<T>> / Func<List<T>> / ... instead

I hadn't thought about trying that, switching to IList has worked 100% and I don't have to change much other than some declarations through my layers. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Aug 26, 2021
pranavkm pushed a commit that referenced this issue Aug 31, 2021
PR Title
Using generic arrays as component parameters resulted in compilation failures which are now fixed.

PR Description
Before it was only possible to have generic parameters like IList<T> / Func<T> but not T[] / IList<T[]> / Func<T[]> because a different check for arrays is required.

Fixes #34711
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Duplicate Resolved as a duplicate of another issue Status: Resolved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants