繁体   English   中英

如何在装饰器中定义事件和属性

[英]How to define events and properties in a decorator

我定义了一个接口,它有一个事件和一个定义如下的属性。

public interface IMyInterface
{
    event EventHandler SomeEvent;
    string GetName();
    string IpAddress { get; set; }
}

然后我创建了一个类并使用它,一切正常。

现在我想使用装饰器扩展这个类。 我不确定如何处理该事件。 对于财产我想我很清楚,只是想确认一下

我如下定义了装饰器类。

public class LoggerDecorator : IMyInterface
{
    private readonly IMyInterface _MyInterface;
    private readonly ILog _MyLog;
    public LoggerDecorator(IMyInterface myInterface, ILog myLog)
    {
        if (myInterface == null)
            throw new ArgumentNullException("IMyInterface is null");
        _MyInterface = myInterface;

        if (myLog == null)
            throw new ArgumentNullException("ILog instance is null");
        _MyLog = myLog;

    }

    public string GetName()
    {
        // If needed I can use log here
        _MyLog.Info("GetName method is called.");
        return _MyInterface.GetName();
    }

    // Is this the way to set properties?
    public string IpAddress
    {
        get
        {
            return _MyInterface.IpAddress;
        }

        set
        {
            // If needed I can use log here
            _MyLog.Info("IpAddress is set.");
            _MyInterface.IpAddress = value;
        }
    }


    // But How to handle this evetn?? Please help. I am not clear about this.
    public event EventHandler SomeEvent;

}

您可以使用addremove将事件的订阅和取消订阅转发到正在装饰的元素。 这样,如果您订阅LoggerDecorator.SomeEvent ,您实际上订阅了内部IMyInterface.SomeEvent

在你的情况下,它应该是这样的:

public class LoggerDecorator : IMyInterface
{
    (...)

    public event EventHandler SomeEvent
    {
        add => _MyInterface.SomeEvent += value;
        remove => _MyInterface.SomeEvent -= value;
    }
}

您不应该处理该事件,您可以引发该事件:

像:

if(SomeEvent != null)
    SomeEvent(this, EventArgs.Empty);

在 C# 6.0 中

SomeEvent?.Invoke(this, EventArgs.Empty);

好的,我想我得到了答案。

以下内容在 ctor 中。

        _MyInterface.SomeEvent += _MyInterface_SomeEvent;

事件处理程序方法如下。

    private void _MyInterface_SomeEvent(object sender, EventArgs e)
    {
        var someEvent = SomeEvent;
        if (someEvent != null)
        {
            someEvent(this, e);
        }
    }

完整的实现如下。

public class LoggerDecorator : IMyInterface
{
    private readonly IMyInterface _MyInterface;
    private readonly ILog _MyLog;
    public LoggerDecorator(IMyInterface myInterface, ILog myLog)
    {
        if (myInterface == null)
            throw new ArgumentNullException("IMyInterface is null");
        _MyInterface = myInterface;

        if (myLog == null)
            throw new ArgumentNullException("ILog instance is null");
        _MyLog = myLog;

        // This is change 1.
        _MyInterface.SomeEvent += _MyInterface_SomeEvent;
    }

    // This is change 2
    private void _MyInterface_SomeEvent(object sender, EventArgs e)
    {
        var someEvent = SomeEvent;
        if (someEvent != null)
        {
            someEvent(this, e);
        }
    }

    public string GetName()
    {
        // If needed I can use log here
        _MyLog.Info("GetName method is called.");
        return _MyInterface.GetName();
    }

    // Is this the way to set properties?
    public string IpAddress
    {
        get
        {
            return _MyInterface.IpAddress;
        }

        set
        {
            // If needed I can use log here
            _MyLog.Info("IpAddress is set.");
            _MyInterface.IpAddress = value;
        }
    }


    public event EventHandler SomeEvent;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM