Invocation of a polymorphic field-like event

血红的双手。 提交于 2019-11-30 10:54:55

Well, you've actually got two field-like events here. Your override will be overriding the add/remove part, but you'll have two fields - one in TableMain and one in TableSub. Only the one in TableSub will ever be non-null, unless the value is set explicitly in TableMain... so if TableMain ever tries to raise the event itself, it won't call the same set of handlers as in TableSub. Basically, it's going to behave strangely.

The right approach is to provide a protected method in TableMain to allow the event to be raised by subclasses:

protected void OnUpdateFilter()
{
    Action handler = UpdateFilter;
    if (handler != null)
    {
        handler();
    }
}

Then make the event non-virtual, and remove the override in TableSub.

Note that your event signature doesn't match the normal convention for events - any reason for not using EventHandler?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!