C#中扩展StringBuilder支持链式方法

本篇体验扩展StringBuilder使之支持链式方法。

这里有一个根据键值集合生成select元素的方法。

private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
{
    var html = new StringBuilder();
    html.AppendFormat("<select id="{0}" name="{0}">", id);
    html.AppendLine();
    
    if(includeUnknown)
    {
        html.AppendLine("	<option>Unknown</option>");
    }
    
    foreach(var opt in options)
    {
        html.AppendFormat("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
        html.AppendLine();
    }
    
    html.AppendLine("</select>");
    
    return html.ToString();
}

以上,

html.AppendFormat("<select id="{0}" name="{0}">", id);
html.AppendLine();

可以对这两个语句封装,扩展StringBuilder。
            
            
改成

public static class StringBuilderExtensions
{
    public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
}

private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
{
    var html = new StringBuilder()
        .AppendFormattedLine("<select id="{0}" name="{0}">", id);

    
    if(includeUnknown)
    {
        html.AppendLine("	<option>Unknown</option>");
    }
    
    foreach(var opt in options)
    {
        html.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
        html.AppendLine();
    }
    
    html.AppendLine("</select>");
    
    return html.ToString();
}

以上,

    if(includeUnknown)
    {
        html.AppendLine(" <option>Unknown</option>");
    }

可以对如上语句进行封装,继续扩展StringBuilder.

public static class StringBuilderExtensions
{
    public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
    
    public static StringBuilder AppendLineWhen(this StringBuilder @this, Func<bool> predicate, string value) =>
    predicate() 
        ? @this.AppendLine(value)
        : @this;
    
}

private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
{
    var html = new StringBuilder()
        .AppendFormattedLine("<select id="{0}" name="{0}">", id)
        .AppendLineWhen(() => includeUnknown, "	<option>Unknown</option>");

    
    foreach(var opt in options)
    {
        html.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
    }
    
    html.AppendLine("</select>");
    
    return html.ToString();
}

public static class StringBuilderExtensions
{
    public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
    
    public static StringBuilder AppendLineWhen(this StringBuilder @this, Func<bool> predicate, string value) =>
    predicate() 
        ? @this.AppendLine(value)
        : @this;
        
    public static StringBuilder AppendWhen(this StringBuilder @this, Func<bool> predicate, Func<StringBuilder, StringBuilder> fn) =>
    predicate()
        ? fn(@this)
        : @this;
    
}

private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
{
    var html = new StringBuilder()
        .AppendFormattedLine("<select id="{0}" name="{0}">", id)
        .AppendWhen(
            () => includeUnknown,
            sb => sb.AppendLine("	<option>Unknown</option>")
        );

    
    foreach(var opt in options)
    {
        html.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value);
    }
    
    html.AppendLine("</select>");
    
    return html.ToString();
}

以上,

    foreach(var opt in options)
    {
        html.AppendFormattedLine(" <option value="{0}">{1}</option>", opt.Key, opt.Value);
    }
    
对遍历语句进行封装,扩展StringBuilder,最终:   

public static class StringBuilderExtensions
{
    public static StringBuilder AppendFormattedLine(this StringBuilder @this, string format, prams object[] args) => @this.AppendFormat(format, args).AppendLine();
    
    public static StringBuilder AppendLineWhen(this StringBuilder @this, Func<bool> predicate, string value) =>
    predicate() 
        ? @this.AppendLine(value)
        : @this;
        
    public static StringBuilder AppendWhen(this StringBuilder @this, Func<bool> predicate, Func<StringBuilder, StringBuilder> fn) =>
    predicate()
        ? fn(@this)
        : @this;
        
    public static StringBuilder AppendSequence<T>(this StringBuilder @this, IEnumerable<T> seq, Func<StringBuilder, T, StringBuilder> fn) => seq.Aggregate(@this, fn);
    
}

private static string BuilderSelectBox(IDictionary<int, string> options, string id, bool includeUnknown)
{
    var html = new StringBuilder()
        .AppendFormattedLine("<select id="{0}" name="{0}">", id)
        .AppendWhen(
            () => includeUnknown,
            sb => sb.AppendLine("	<option>Unknown</option>")
        )
        .AppendSequence(options, (sb, opt) => sb.AppendFormattedLine("	<option value="{0}">{1}</option>", opt.Key, opt.Value))
        .AppendLine("</select>")
        .ToString();
}
原文地址:https://www.cnblogs.com/darrenji/p/5271696.html