该事件只能出现在+ =或-= dotnetstandard 2.1的左侧 [英] The event can only appear on the left hand side of += or -= dotnetstandard 2.1

查看:116
本文介绍了该事件只能出现在+ =或-= dotnetstandard 2.1的左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是点网标准2.1和c#8,我想为我的类(接口)创建一个事件,我遵循

I'm using dot net standard 2.1 and c# 8, I want to create an event for my class (interface), I follow this tutorial and I wrote an interface:

using System;
using Crawler.Paging;

namespace Crawler
{
    public interface ICrawler
    {
        public event EventHandler NextPage;
        protected virtual void OnNextPage(EventArgs e)
        {

            EventHandler handler = NextPage;
            handler?.Invoke(this,e);
        }
        void Paging(IPaging paging);
    }
}

但是给我一个错误:

错误事件"ICrawler.NextPage"只能出现在左侧 + =或-=

Error The event 'ICrawler.NextPage' can only appear on the left hand side of += or -=

我进行了这次培训,那么问题出在哪里?

I proceeded with this training, so where is the problem?

推荐答案

原因

一个简单的事件定义(由类中的;结束)由两部分组成,它们是事件,它既包含添加/删除访问器(方法),也包含处理程序委托.

Reason

A simple event definition ending with ; in a class is made of 2 parts, which are the event, which only contains both add/remove accessors (methods), and the handler delegate.

class Foo
{
    public event EventHandler Bar;
}

等于

class Foo
{
    //The event
    public event EventHandler Bar
    {
        add => _bar += value;
        remove => _bar -= value;
    }

    //The handler value
    private EventHandler _bar;
}

请注意,无论事件定义的访问修饰符如何,后备字段始终为private.因此,Bar?.Invoke()实际上是直接访问处理程序委托,而不是访问器,并且只能在类本身内完成.

Note that the backing field is always private, regardless of the access modifier of the event definition. So Bar?.Invoke() is actually accessing the handler delegate directly, not the accessors, and can only be done within the class itself.

但是接口中以;结尾的简单事件定义只是抽象事件,它只包含添加/删除抽象访问器(抽象方法).

But a simple event definition ending with ; in an interface is only the abstract event, which only contains both add/remove abstract accessors (abstract methods).

interface IFoo
{
    event EventHandler Bar;
}

等于

interface IFoo
{
    public abstract EventHandler Bar;
    //The following syntax is invalid but shows how it works.
    /*
    event EventHandler Bar
    {
        abstract add;
        abstract remove;
    }
    */
}

C#中的默认接口实现功能不会对其进行更改,因为接口不能包含任何字段(定义C#中的接口是什么).只要处理程序委托不存在,就无法直接访问它,因此Bar?.Invoke()无效.

The default interface implementation feature in C# does not breaking-change it, for an interface cannot contain any field (which defines what interface in C# is). As long as the handler delegate does not exist, it is not possible to access it directly, therefore Bar?.Invoke() is invalid.

有一个解决方法,是使用一个带有抽象属性的手动实现的事件(这也是默认实现)作为处理程序委托:

There is a workaround by using a manually implemented event (which is also a default implementation) with an abstract property as the handler delegate:

interface IFoo
{
    protected EventHandler BarHandler { get; set; }

    event EventHandler Bar
    {
        add => BarHandler += value;
        remove => BarHandler -= value;
    }
}

class Foo : IFoo
{
    EventHandler IFoo.BarHandler { get; set; }
}

以便默认方法实现中的其他地方可以调用该事件:

So that somewhere else in a default method implementation could invoke the event:

var handler = BarHandler;
handler?.Invoke(this, e);

这篇关于该事件只能出现在+ =或-= dotnetstandard 2.1的左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆