Blazor 不可为空警告的最佳实践

Tac*_*aco 4 .net-core blazor

使用 blazor,我在代码周围收到不可为空的警告。这些警告似乎是错误的,但是解决它们会引入大量代码,其唯一目的是隐藏警告,而值永远不会为空。

解决或隐藏这些警告的最佳实践是什么?

例子:

[Inject] private IStringLocalizer<Element> L { get; set; }
Element.razor.cs(5, 50): [CS8618] Non-nullable property 'L' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Run Code Online (Sandbox Code Playgroud)

mez*_*tou 9

由于 Blazor 在执行代码之前始终会将该属性分配给非空值,因此使用 禁用警告是安全的= default!

[Inject] private IStringLocalizer<Element> L { get; set; } = default!;
Run Code Online (Sandbox Code Playgroud)