zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    HTML5 文件上传
    Vue-Router模式、钩子
    网络基础协议随笔
    Vue-Router基础使用
    vue中mixin的一点理解
    纯css3跑马灯demo
    Charles使用笔记
    提个醒。。。
    本机未装Oracle数据库时Navicat for Oracle 报错:Cannot create oci environment 原因分析及解决方案
    easyUI datagrid 清空
  • 原文地址:https://www.cnblogs.com/darrenji/p/5271696.html
Copyright © 2011-2022 走看看