CS8622 参数“sender”类型中引用类型的可为空性

问题描述 投票:0回答:1

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

void myhander 的参数“sender”类型中引用类型的可为空性与目标委托不匹配(可能是因为可为空性属性)

代码是

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}");
}

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

c# compiler-errors
1个回答
32
投票

这是因为

BindingManagerDataErrorEventHandler
在定义中需要可以为空的发送者。 您可以在这里阅读: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}");
}
© www.soinside.com 2019 - 2024. All rights reserved.