Skip to content

Blazor: inherits typeparam from another property "the type t must be a reference type in order to use it as a parameter" #11505

@itarsonmg

Description

@itarsonmg

the example:

MyComponent.razor

@inherits ITComponentBase
@typeparam T

MyComponent.razor.cs

[CascadingTypeParameter(nameof(T))]
public partial class MyComponent<T> : ComponentBase 
{
    [Parameter]
    public MyQueryCommand<T> QueryCommand { get; set; } = default!;
}

public class MyQueryCommand<T>  
{

}

Page.razor

<MyComponent QueryCommand="new MyQueryCommand<AuthUser>()">
    
</MyComponent>

thats works perfect, but when I put a restriction in paramtype T, fails

MyComponent.razor.cs

[CascadingTypeParameter(nameof(T))]
public partial class MyComponent<T> : ComponentBase where T : IDomain
{
    [Parameter]
    public MyQueryCommand<T> QueryCommand { get; set; } = default!;
}

public class MyQueryCommand<T>  where T : IDomain
{

}

The sourceGenerator: error CS0314: El tipo 'T' no se puede usar como parámetro de tipo 'T' en el tipo o método genérico 'MyQueryCommand<T>'. No hay conversión boxing ni conversión de parámetro de tipo de 'T' a 'ITProject.Domain.IDomain'.

Page.razor.g.cs

        public static void CreateMyComponent_0<T>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::ITProject.Components.MyQueryCommand<T> __arg0)
            where T : global::ITProject.Domain.IDomain
        {
              ....................
        }

        public static void CreateMyComponent_0_CaptureParameters<T>(global::ITProject.Components.MyQueryCommand<T> __arg0, out global::ITProject.Components.MyQueryCommand<T> __arg0_out)
        {
            __arg0_out = __arg0;
        }

if I put a restriction in CreateMyComponent_0_CaptureParameters<T> same as it is in CreateMyComponent_0<T> all works

public static void CreateMyComponent_0_CaptureParameters<T>(global::ITProject.Components.MyQueryCommand<T> __arg0, out global::ITProject.Components.MyQueryCommand<T> __arg0_out)
where T : global::ITProject.Domain.IDomain
        {
            __arg0_out = __arg0;
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-compilerUmbrella for all compiler issues

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions