在C#中使用委托作为属性是否正确?

时间:2012-07-05 15:40:25

标签: c# properties delegates

我需要创建一个包含两个属性的类:

  1. LogOutput
  2. ExceptionOutput
  3. 这些属性(Actions<>)根据目标函数发送消息或异常。此目标函数通过属性设置。

    目前,我有这个功能代码:

        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);
            }
        }
    

    这个型号是对的吗?还有另一种方法吗?

2 个答案:

答案 0 :(得分:8)

  

这个型号是对的吗?还有另一种方法吗?

这必然是没有错的。但是,events 可能是处理此问题的更常用方法,因为您在此方案中有效地将委托用作事件。

使用事件确实有一个显着的优点(可能),因为您也可以轻松拥有多个订阅者,这样可以让多个项目“监听”异常或日志消息变得简单。 (*虽然这也适用于代表,但它不是使用代理的标准方法..)

答案 1 :(得分:1)

抱歉offtopic但使用StringBuilder string不喜欢编辑

            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();