C#:什么是虚拟事件,如何使用? [英] C#: What are virtual events and how can they be used?

查看:139
本文介绍了C#:什么是虚拟事件,如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虚拟事件如何工作?你会怎么覆盖它?该怎么工作?你会在什么情况下这样做?



例如,对于受保护的OnEvent方法来说,可以替代它吗?那么继承类可以只是覆盖事件并直接提交呢?或者是错误还是不行?



MSDN对此说:


可以通过使用virtual关键字将事件标记为虚拟事件。这使得派生类可以使用override关键字来覆盖事件行为。覆盖虚拟事件的事件也可以被密封,这表明对于派生类,它不再是虚拟的。


但是,让我更聪明密封的东西很明显。



注意:我看过虚拟事件如何在C#?中出现,但虚拟事件的工作原理并不是这样。相反,这个人是如何得到使用它们的结果。试图找出他的例子和答案中的虚拟事件,但是真的没有什么意义。

解决方案

虚拟事件只是在派生类中可以被覆盖的事件。



您是否满意于虚拟属性,可以覆盖的getter和setter?如果是这样,你可以用完全相同的方法来考虑一个虚拟事件:而不是一个getter和setter,有一个add操作和一个remove操作。这些可以是虚拟的,因此处理多边形。您实现它们的方式与实现任何其他虚拟/覆盖成员的方式相同。



示例:

 使用系统; 

class Base
{
public virtual event EventHandler Foo
{
add
{
Console.WriteLine(Base Foo add);
}
删除
{
Console.WriteLine(Base Foo.remove called);
}
}
}

类派生:Base
{
public override event EventHandler Foo
{
添加
{
Console.WriteLine(Derived Foo.add called);
}
删除
{
Console.WriteLine(Derived Foo.remove called);
}
}
}

class Test
{
static void Main()
{
Base x = new Derived();

x.Foo + =(sender,args)=> {};
}
}

请注意,事件本身对发生的事情不负责任当它被提高 - 只是添加/删除方面。 (在C#中,无论如何,CLR本身都有提高的概念,但我们暂时忽略)。



您可能还想阅读我关于事件的文章,如果你对一个事件和一个委托人之间的差异有点模糊。 / p>

我个人觉得我很想要一个虚拟活动

How does a virtual event work? How would you override it? How would that work? And in what cases would you do that?

Would it for example be an ok replacement for protected OnEvent methods? So that inheriting classes could just override the event and raise it directly? Or would that be wrong or just not work?

The MSDN says this about it:

An event can be marked as a virtual event by using the virtual keyword. This enables derived classes to override the event behavior by using the override keyword. An event overriding a virtual event can also be sealed, which specifies that for derived classes it is no longer virtual.

But that didn't make me much wiser. The sealed stuff is obvious though.

Note: I've seen the How virtual events work in C# ? question, but it wasn't really about how virtual events work. Rather it was how that person got the result he got from using them. Tried to figure out what virtual events were from his example and the answers, but couldn't really make sense out of it.

解决方案

A virtual event is simply one which can be overridden in a derived class.

Are you happy with the concept of a virtual property, with a getter and setter which can be overridden? If so, you can think of a virtual event in exactly the same way: instead of a getter and setter, there's an "add" operation and a "remove" operation. These can be virtual, so handled polymorphically. You implement them the same way you implement any other virtual/overridden member.

Example:

using System;

class Base
{
    public virtual event EventHandler Foo
    {
        add
        {
            Console.WriteLine("Base Foo.add called");
        }
        remove
        {
            Console.WriteLine("Base Foo.remove called");
        }
    }
}

class Derived : Base
{
    public override event EventHandler Foo
    {
        add
        {
            Console.WriteLine("Derived Foo.add called");
        }
        remove
        {
            Console.WriteLine("Derived Foo.remove called");
        }
    }
}

class Test
{
    static void Main()
    {
        Base x = new Derived();

        x.Foo += (sender, args) => {};
    }
}

Note that the event itself is not responsible for what happens when it is raised - just the add/remove side. (In C#, anyway; the CLR itself has the notion of raising, but we'll ignore that for the moment.)

You may also want to read my article on events if you're slightly hazy on the difference between an event and a delegate.

Personally I find it very rare that I want a virtual event.

这篇关于C#:什么是虚拟事件,如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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