2016-08-11 51 views
0

我定義了一個接口,它具有如下定義的事件和屬性。如何在裝飾器中定義事件和屬性

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; 

} 

回答

1

你不應該處理該事件,您可以引發事件:

像:

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

C#6.0

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

好吧,我想我明白了。讓我嘗試。 – VivekDev

+1

定義事件時。其他對象可以註冊到它。所以你的對象可以在不知道/限制接收者類的情況下進行「回調」。這樣你可以重用可以通知/調用方法的類。 –

0

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

以下內容在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; 

} 
相關問題