2012-07-05 34 views
4

我需要創建一類具有兩個屬性:在C#中使用委託作爲屬性是否正確?

  1. LogOutput
  2. ExceptionOutput

這些屬性(操作<>)發送消息或根據目標功能的異常。這個目標函數是通過屬性設置的。

目前,我有這樣的功能代碼:

public class Output 
    { 
     private Action<string> logOutput; 
     private Action<Exception, string> exceptionOutput; 

     public Action<string> LogOutput { set { this.logOutput = value; } get { return this.logOutput; } } 
     public Action<Exception, string> ExceptionOutput { set { this.exceptionOutput = value; } get { return this.exceptionOutput; } } 

     public Output() : this(null, null) { } 

     public Output(Action<string> logAction, Action<Exception, string> exceptionAction) 
     { 
      this.logOutput = logAction; 
      this.exceptionOutput = exceptionAction; 
     } 


     public void WriteLogMessage(string format, params object[] args) 
     { 
      if (this.logOutput != null) 
       logOutput(string.Format(format, args)); 
     } 

     public void WriteExceptionMessage(Exception ex, string format, params object[] args) 
     { 
      if (this.exceptionOutput != null) 
       exceptionOutput(ex, string.Format(format, args)); 
     } 
    } 

這是我的表單代碼:

private void MainForm_Load(object sender, EventArgs e) 
    { 
     // my Output object 
     Output myOutput = new Output(); 

     // set properties 
     myOutput.ExceptionOutput = this.WriteExceptionMessageToTextBox; 
     myOutput.LogOutput = this.WriteLogMessageToTextBox; 

     // test 
     myOutput.WriteLogMessage("this is my log message to text box"); 
     myOutput.WriteExceptionMessage(new Exception("this is my exception"), "this is my exception message to text box"); 
    } 

    private void WriteLogMessageToTextBox(string message) 
    { 
     // nothing to do here 
     if (this.txtBox.IsDisposed) 
      return; 

     if (this.InvokeRequired) 
     { 
      BeginInvoke(new MethodInvoker(delegate() { WriteLogMessageToTextBox(message); })); 
     } 
     else 
     { 
      // write to text box 
      this.txtBox.AppendText(message + Environment.NewLine); 
     } 
    } 

    private void WriteExceptionMessageToTextBox(Exception ex, string message) 
    { 
     // nothing to do here 
     if (this.txtBox.IsDisposed) 
      return; 

     if (this.InvokeRequired) 
     { 
      BeginInvoke(new MethodInvoker(delegate() { WriteExceptionMessageToTextBox(ex, message); })); 
     } 
     else 
     { 
      string msg = ""; 
      msg += string.Format("Program:{0}", message); 
      msg += string.Format("Message{0}", ex.Message); 
      msg += string.Format("StackTrace:{0}", ex.StackTrace); 
      msg += string.Format("Source:{0}", ex.Source); 

      // write to text box 
      this.txtBox.AppendText(msg + Environment.NewLine); 
     } 
    } 

這是正確的這種模式?還有另一種方法可以做到這一點?

+0

如果「MyOutput中.ExceptionOutput = null「是可以接受的,那麼事件和委託並沒有什麼不同。事件是「正確的」,所以代表也是如此。 – 2012-07-05 15:42:14

+1

它工作嗎?如果是這樣,這可能更適合CodeReview。 – 2012-07-05 15:43:16

回答

8

這是正確的這種模式?還有另一種方法可以做到這一點?

沒有什麼不對的,一定。然而,events可以是處理這一種比較常見的方法,因爲你正在有效地利用委託作爲在這種情況下的事件。

使用事件確實有一個顯著的優勢(可能),因爲你也可以輕鬆擁有多個訂戶,這將使它簡單,允許一個以上的項目爲「聽」到的異常或日誌消息。 (*雖然這與代表作品,以及,它不會作爲的方式使用委託標準..)

1

對不起offtopic但使用StringBuilderstring不喜歡編輯

  string msg = ""; 
      msg += string.Format("Program:{0}", message); 
      msg += string.Format("Message{0}", ex.Message); 
      msg += string.Format("StackTrace:{0}", ex.StackTrace); 
      msg += string.Format("Source:{0}", ex.Source); 


      StringBuilder sb = new StringBuilder(); 
      sb.Append(string.Format("Program:{0}", message)); 
      sb.Append(string.Format("Message{0}", ex.Message)); 
      sb.Append(string.Format("StackTrace:{0}", ex.StackTrace)); 
      sb.Append(string.Format("Source:{0}", ex.Source)); 
      string result = sb.ToString(); 
+0

您可以更改.Append()=> .AppendFormat(),並鏈接它們。 – 2012-07-05 15:52:43

+1

鑑於字符串不可能*很大,並且只有四個連接,所以這可能不是一個重大問題。這也沒有以任何方式回答這個問題,它只是一個評論。 – Servy 2012-07-05 15:53:21

+0

評論有錯誤的地方 – 2012-07-05 16:04:18