Skip to content

Fix setting nullable Action parameters on components #23892

@jontavakoli

Description

@jontavakoli

When passing an Action from one Component to another component if the value is null the code throws the following exception: System.ArgumentException: 'Delegate to an instance method cannot have null 'this'.'

This is caused by the generated code, because it tries to create an instance of the Action delegate instead of just passing the value along as it is:

Component A - Generated Code (A.razor.g.cs)

public partial class A : Microsoft.AspNetCore.Components.ComponentBase
    {
        protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
        {
            __builder.OpenComponent<B>(0);
            __builder.AddAttribute(1, "When", new System.Action(When));
            __builder.CloseComponent();
        }

	[Parameter]
	public Action When { get; set; }

}

Component A (A.razor)

@code{

	[Parameter]
	public Action When { get; set; }

}

<B When="@When" />

Component B (B.razor)

@code{

	[Parameter]
	public Action When { get; set; }

}

<span>Empty</span>

Is there a way to override the code generation logic to stop this __builder.AddAttribute(1, "When", new System.Action(When)); and instead do __builder.AddAttribute(1, "When", When);.

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedaffected-mediumThis issue impacts approximately half of our customersarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-component-modelAny feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc)feature-renderingFeatures dealing with how blazor renders componentsseverity-minorThis label is used by an internal tool

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions