最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Blazor component with func parameter that has nullable generic type parameters - Stack Overflow

programmeradmin5浏览0评论

I'm creating a Blazor component and running into nullable type issues

MyComponentTest.razor(3,36): error CS0407: 'int? MyComponentTest.DivideByFour(string?)' has the wrong return type

In MyComponentTest.razor, it's expecting me to pass a Func<string, T> to ToValueFunc, even though in MyComponent.razor ToValueFunc is defined as being Func<string?, T?>.

Three things worth mentioning:

  • This is just a minimal example to illustrate the issue and is not the same as my production code
  • The .csproj specifies <TargetFramework>net9.0</TargetFramework> and <Nullable>enable</Nullable>
  • Type T should be able to be either a reference or a value type

MyComponent.razor:

@typeparam T

<div>@ToValueFunc(Text)</div>

@code {
    [Parameter]
    public string? Text { get; set; }

    [Parameter, EditorRequired]
    public Func<string?, T?> ToValueFunc { get; set; } = default!;
}

MyComponentTest.razor:

@page "/mycomponenttest"

<MyComponent T="int" ToValueFunc="@DivideByFour" Text="@_text" />

@code {
    private string? _text = "4";
    
    private int? DivideByFour(string? text)
        => int.TryParse(text, out int value) ? value / 4 : null;
}

Why is this happening and is there any way to get it to obey the nullability specified in MyComponent.razor?

I'm creating a Blazor component and running into nullable type issues

MyComponentTest.razor(3,36): error CS0407: 'int? MyComponentTest.DivideByFour(string?)' has the wrong return type

In MyComponentTest.razor, it's expecting me to pass a Func<string, T> to ToValueFunc, even though in MyComponent.razor ToValueFunc is defined as being Func<string?, T?>.

Three things worth mentioning:

  • This is just a minimal example to illustrate the issue and is not the same as my production code
  • The .csproj specifies <TargetFramework>net9.0</TargetFramework> and <Nullable>enable</Nullable>
  • Type T should be able to be either a reference or a value type

MyComponent.razor:

@typeparam T

<div>@ToValueFunc(Text)</div>

@code {
    [Parameter]
    public string? Text { get; set; }

    [Parameter, EditorRequired]
    public Func<string?, T?> ToValueFunc { get; set; } = default!;
}

MyComponentTest.razor:

@page "/mycomponenttest"

<MyComponent T="int" ToValueFunc="@DivideByFour" Text="@_text" />

@code {
    private string? _text = "4";
    
    private int? DivideByFour(string? text)
        => int.TryParse(text, out int value) ? value / 4 : null;
}

Why is this happening and is there any way to get it to obey the nullability specified in MyComponent.razor?

Share Improve this question edited Mar 25 at 9:45 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 24 at 18:03 Dustin NieffeneggerDustin Nieffenegger 6508 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Your Func specifies a nullable T?, so your MyComponent declaration should be:

<MyComponent T="int?" ToValueFunc="@DivideByFour" Text="@_text" />

Or let the compiler sort it out with:

<MyComponent ToValueFunc="@DivideByFour" Text="@_text" />

You should also probably null check ToValueFunc like this in MyComponent:

@typeparam T

<div>@(Text is null ?  "Not Set" : ToValueFunc(Text))</div>

@code {
    [Parameter] public string? Text { get; set; }

    [Parameter, EditorRequired] public Func<string?, T?> ToValueFunc { get; set; } = default!;

    protected override void OnInitialized()
    {
        ArgumentNullException.ThrowIfNull(ToValueFunc, nameof(ToValueFunc));
    }
}
发布评论

评论列表(0)

  1. 暂无评论