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

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

问题描述

如何做一个虚拟活动工作?你会如何​​改写呢?如何将这项工作?在什么情况下,你会怎么做呢?

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

它例如将是保护的OnEvent方法的确定替换?这样继承类可以只重载事件,并直接提高了吗? ?还是会认为是错误的或只是不工作

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?

在MSDN说,这一下:

The MSDN says this about it:

事件可以被标记为通过使用虚拟关键字的虚拟事件。这使派生类通过使用override关键字重写事件行为。重写虚拟事件的事件也可以被密封,它指定为派生类不再虚拟

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.

注:我见过的虚拟事件在C#?的问题是如何工作的,但它不是真正的虚拟事件是如何工作的。相反,它是人是如何得到他从使用它们得到的结果。试图找出哪些虚拟事件是从他的榜样和答案,但不能真正弄明白它。

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.

您满意的概念虚拟财产,具有getter和setter可以重写?如果是这样,你能想到在完全相同的方式虚拟事件:而不是一个getter和setter的,有一个增加操作和删除操作。这些可以是虚拟的,这么多态形式处理。你实现它们在实施任何其他虚拟/重写的成员以同样的方式。

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.

例如:

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) => {};
    }
}

请注意,该事件本身并不负责会发生什么当它被提出 - 只要添加/删除的一面。 (在C#中,反正;在CLR本身具有提高的概念,但我们会忽略的时刻)

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天全站免登陆