2013-02-27 48 views
0

我試圖在cSharp中引發事件來通知我的應用程序中的代碼更改,以便具有tellDontAsk方案。使用事件來應用TellDontAsk模式

我必須從事件類實現

public class SimpleTellDontAsk : ISomeEvent 
{ 
    public void doSomething(string text, EventHandlerArgs args) 
    { 
     Console.WriteLine("Email this message {0}", text); 
     //sending message with email 
     args.IsDo = true; 
     RaiseEvent(this, args); 
    } 

    public event EventHandler RaiseEvent; 
} 

我這樣定義如下

public interface ISomeEvent 
{ 
    event EventHandler RaiseEvent; 
} 

public class SomeEvent : ISomeEvent 
{ 
    public event EventHandler RaiseEvent; 
} 

public class EventHandlerArgs : EventArgs 
{ 
    public bool IsDo { get; set; } 
} 

我嘗試使用我的代碼與NUnit測試我的事件類的簡單類

[TestFixture] 
public class TestSimpleTellDontAsk 
{ 
    [Test] 
    public void Given_Text_When_doSomething_Then_ShouldPubliushArgs() 
    { 
     var tellDontAsk = new SimpleTellDontAsk(); 
     var senderEventHandlerArgs = new EventHandlerArgs(); 
     tellDontAsk.doSomething("test message", senderEventHandlerArgs); 
     Assert.IsTrue(senderEventHandlerArgs.IsDo); 
    } 
} 

當我運行測試時,它會出現空引用異常

System.NullReferenceException:未將對象引用設置爲對象的實例。

我相信有東西丟失,但無法弄清楚,任何想法?

+0

您是否在調試器中運行代碼並確定引發此異常的行? – 2013-02-27 11:13:51

+0

是的它在RaiseEvent(this,args);行 – wikinevis 2013-02-27 11:15:42

+0

您通常在觸發事件時執行類似[this](http://stackoverflow.com/a/672666/238902)的操作。這至少應該確保你不會得到nullreference異常 – Default 2013-02-27 11:20:14

回答

2

的RaiseEvent不附

bool eventFired = false; 

var tellDontAsk = new SimpleTellDontAsk(); 
tellDontAsk.RaiseEvent += (o, e) => 
      { 
       if (e.IsDo) 
        eventFired = true; 
      }; 
tellDontAsk.doSomething("test message"); 
Assert.IsTrue(eventFired); 

另外,如果你想使用自己的EventArgs「EventHandlerArgs」你應該去通用事件處理程序。

eventargs不應該在方法的參數中提供,它們應該在我們需要提升事件時創建。

public class SimpleTellDontAsk : ISomeEvent 
{ 
    public void doSomething(string text) 
    { 
     Console.WriteLine("Email this message {0}", text); 
     //sending message with email 
     if (RaiseEvent != null) //Check if we have listeners 
     { 
      EventHandlerArgs args = new EventHandlerArgs(); 
      args.IsDo = true; 
      RaiseEvent(this, args); 
     } 
    } 
    public event EventHandler<EventHandlerArgs> RaiseEvent; 
} 
1

RaiseEvent沒有附加處理程序,修改了附加默認處理程序的代碼,我仍然不清楚它是如何實現的告訴別問原則,你只是檢查IsDo屬性,ISomeEvent有什麼用?

public interface ISomeEvent 
{ 
    event Action<ISomeEvent, EventHandlerArgs> RaiseEvent; 
} 

public class SomeEvent : ISomeEvent 
{ 
    public event Action<ISomeEvent, EventHandlerArgs> RaiseEvent; 
} 
public class EventHandlerArgs : EventArgs 
{ 
    public bool IsDo { get; set; } 
} 

public class SimpleTellDontAsk : ISomeEvent 
{ 
    public SimpleTellDontAsk() 
    { 
     RaiseEvent = (s,e) => { }; 
    }         
    public void doSomething(string text, EventHandlerArgs args) 
    { 
     Console.WriteLine("Email this message {0}", text); 
     //sending message with email 
     args.IsDo = true; 
     RaiseEvent(this, args); 
    } 

    public event Action<ISomeEvent, EventHandlerArgs> RaiseEvent; 
} 
相關問題