继承和放大器;活动 [英] Inheritance & Events

查看:145
本文介绍了继承和放大器;活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有一个接口,像这样定义的:

Hello everyone I have an interface defined like so:

public interface IRipper
{
    IRipperHost Host { get; set; }

    void Rip(FileStream stream);

    event RipperEventHandler OnStart;
    event RipperEventHandler OnComplete;
    event RipperEventHandler OnProgressChanged;        
}

public delegate void RipperEventHandler(object sender, RipperEventArgs e);

和一个基类实现该接口

public class RipperBase : IRipper
{

    public void Rip(FileStream stream)
    {
        throw new System.NotImplementedException();
    }
    public event RipperEventHandler PostRipEvent;
    event RipperEventHandler IRipper.OnComplete
    {
        add
        {
            if (PostRipEvent != null)
            {
                lock (PostRipEvent)
                {
                    PostRipEvent += value;
                }
            }
            else
            {
                PostRipEvent = value;
            }
        }
        remove
        {
            if (PostRipEvent != null)
            {
                lock (PostRipEvent)
                {
                    PostRipEvent -= value;
                }
            }
        }
    }
    ... and so on
}

我的问题是如何将从RipperBase继承并重写的BaseClass瑞普方法。在现实中该功能应该是抽象的 - 但我不知道该怎么做,与接口。

My question is how would you inherit from RipperBase and override the BaseClass Rip method. in reality that function should be abstract - but i don't know how to do that with interfaces.

此外怎能叫我从RipperBase派生类的基类的事件处理程序?我想这

Also how can i call the base classes event handler from the class derived from RipperBase? I tried this

    RipperEventHandler handler = PreRipEvent;
    if (handler != null)
    {
        handler(this, new RipperEventArgs { Message = "Started Ripping file to CSV!" });
    }

    handler = RipProgEvent;



这一点,但我得到了事件只能出现在左手侧+ =或 - =。误差

this but I'm getting the "event can only appear on the left hand side of += or -=." error

任何帮助将不胜感激!我试图让这个可伸缩越好。

any help would be greatly appreciated! I'm trying to make this as scalable as possible.

推荐答案

我会做这样的:

    public abstract class RipperBase : IRipper
    {
        public void Rip( Filestream fs )
        {
             RipCore (fs);
             OnCompleted();
        }

        protected abstract void RipCore(Filestream fs);

        public event EventHandler<RipperEventArgs> Completed;

        protected void OnCompleted
        {
           if( Completed != null )
           {
               // TODO: make this Threadsafe.
               Completed (this, new RipperEventArgs( ... ) );
           }
        }
}



我已经声明的类作为抽象的,因为我相信,你不能从实例化对象RipperBase?

I've declared the class as abstract, since I presume that you cannot instantiate objects from RipperBase ?

这从RipperBase继承的类必须覆盖RipCore方法,并实施正确的功能。
RIP是调用RipCore方法的方法,而且还提高了完成事件处理程序。 (也许你不希望这样做,我不知道)。

Classes that inherit from RipperBase have to override the RipCore method, and implement the correct functionality. Rip is the method that calls the RipCore method, and it also raises the Completed eventhandler. (Maybe you do not want to do this, i dunno).

您也可以确保翻录方法调用另一个线程RipCore方法,如果你想要的。

You can also make sure that the Rip method calls the RipCore method on another thread if you want.

有关的事件,创建在可用于提高活动的基类保护方法。如果你想在一个子类,以提高的情况下,你只需要调用此方法。

For the events, I create a protected method in the base class that can be used to raise the events. If you want to raise the event in a subclass, you just call this method.

这篇关于继承和放大器;活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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