2012-07-05 72 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