CS8622 参数类型中引用类型的可空性 'sender'

CS8622 Nullability of reference types in type of parameter 'sender'

我在我的 net6.0-windows SDK 项目中打开了 TreatWarningsAsErrors 并且正在尝试解决错误

Nullability of reference types in type of parameter 'sender' of void myhander doesnt match the target delegate (possibly because of nullability attributes)

密码是

pricingBinder = new BindingSource() { DataSource = _pricingbo };
if (pricingBinder_DataError != null)
{
    pricingBinder.DataError -= pricingBinder_DataError;
    pricingBinder.DataError += pricingBinder_DataError;
}

事件处理程序是

private void pricingBinder_DataError(object sender, BindingManagerDataErrorEventArgs e)
{
    throw new MyGeneralException("## pricingBinder_DataError {0} | {1}");
}

我希望它与检查我的事件处理程序是否可以为 null 有关,但我不确定如何执行此操作。

这是因为 BindingManagerDataErrorEventHandler 在定义中要求可以为 null 的发件人。 你可以在这里阅读:BindingManagerDataErrorEventHandler

因此您需要更改您的代码:

private void pricingBinder_DataError(object sender, BindingManagerDataErrorEventArgs e)
{
    throw new MyGeneralException("## pricingBinder_DataError {0} | {1}");
}

private void pricingBinder_DataError(object? sender, BindingManagerDataErrorEventArgs e)
    {
        throw new MyGeneralException("## pricingBinder_DataError {0} | {1}");
    }