重构多个StringBuilder.Append语句

时间:2011-10-28 08:42:28

标签: c# .net refactoring

今天我看到了这个糟糕的代码结构,从那以后我开始认为看这个代码的方法真的很尴尬和可怕。代码如下:

StringBuilder body = new StringBuilder();       

            #region General
            body.Append("General information:");
            body.Append('*');
            body.Append('*');
            body.Append("Exception:         ");
            body.Append(m_ExceptionInfo.Exception.GetType().ToString());
            body.Append('*');
            body.Append("Message:           ");
            body.Append(m_ExceptionInfo.Exception.Message);
            body.Append('*');
            body.Append("Method:            ");
            body.Append(m_ExceptionInfo.GetMethodName(m_ExceptionInfo.Exception));
            body.Append('*');
            body.Append("Class:             ");
            body.Append(m_ExceptionInfo.GetClassName(m_ExceptionInfo.Exception));
            body.Append('*');
            body.Append("Assembly:          ");
            body.Append(m_ExceptionInfo.AssemblyName);
            body.Append('*');
            body.Append("App-Domain:        ");
            body.Append(m_ExceptionInfo.AppDomainName);
            body.Append('*');
            body.Append("Source-File:       ");
            body.Append(m_ExceptionInfo.GetFileName(m_ExceptionInfo.Exception));
            body.Append('*');
            body.Append("Line/Row:          ");
            body.Append(
                m_ExceptionInfo.GetFileLineNumber(m_ExceptionInfo.Exception).ToString(currentNumberFormatInfoProvider));

我们这样做是为了在UI中自定义显示的错误消息框信息。因此,我们正在准备一个包含如此多信息的字符串。但对我来说,看看这段代码感觉很糟糕,并且不知道如何重构它。

任何帮助表示赞赏!感谢

3 个答案:

答案 0 :(得分:3)

使用StringBuilder.AppendFormat()方法:

StringBuilder body = new StringBuilder();    
body.AppendFormat("Exception: {0}, Message: {1}{2}Class: {3}, Assembly: {4}{5}", 
                            m_ExceptionInfo.Exception.GetType(),
                            m_ExceptionInfo.Exception.Message,
                            Environment.NewLine,                              
                            m_ExceptionInfo.GetClassName(...),
                            m_ExceptionInfo.AssemblyName,
                            Environment.NewLine);

body.AppendFormat("App-Domain: {0}, Source-File: {1}{2}",
                            m_ExceptionInfo.AppDomainName,
                            m_ExceptionInfo.GetFileName(...),
                            Environment.NewLine);

答案 1 :(得分:1)

为什么不创建一个简单的字符串对集合,然后遍历它们来构建实际的字符串?例如:

 Dictionary<string, string> info = new Dictionary<string, string>();
 info.Add("General information", "*");
 info.Add("Exception", m_ExceptionInfo.Exception.GetType().ToString());
 info.Add("Message",   m_ExceptionInfo.Exception.Message);
 //etc

 StringBuilder body = new StringBuilder();  
 foreach(KeyValuePair<string, string> stringPair in info)
     body.AppendFormat("{0}:{1, 20}", stringPair.Key, stringPair.Value);

答案 2 :(得分:0)

为每个添加的信息位创建具有有意义名称的单独方法会使这种方法看起来更好:

private void AppendExceptionMessage(StringBuilder builder)
{
    builder.Append("Message:           ");
    builder.Append(m_ExceptionInfo.Exception.Message);
    builder.Append('*');
}

private void AppendMethodInfo(StringBuilder builder)
{
    builder.Append("Method:            ");
    builder.Append(m_ExceptionInfo.GetMethodName(m_ExceptionInfo.Exception));
    builder.Append('*');
}

body.Append("General information:");
body.Append('*');
body.Append('*');
AppendExceptionMessage(body);
AppendMethodInfo(body);