本篇体验扩展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("\t<option>Unknown</option>");
} foreach(var opt in options)
{
html.AppendFormat("\t<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("\t<option>Unknown</option>");
} foreach(var opt in options)
{
html.AppendFormattedLine("\t<option value=\"{0}\">{1}</option>", opt.Key, opt.Value);
html.AppendLine();
} html.AppendLine("</select>"); return html.ToString();
}

以上,

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

以上,

foreach(var opt in options)
    {
        html.AppendFormattedLine("\t<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("\t<option>Unknown</option>")
)
.AppendSequence(options, (sb, opt) => sb.AppendFormattedLine("\t<option value=\"{0}\">{1}</option>", opt.Key, opt.Value))
.AppendLine("</select>")
.ToString();
}

C#中扩展StringBuilder支持链式方法的更多相关文章

  1. .NetCore 中扩展ExceptionLess 实现链式方法添加操作日志

    在使用ExceptionLess添加日志的时候,发现还是有一些写法上的个人觉得不爽的地方,比如添加Info日志 ExceptionlessClient.Default.CreateLog(source ...

  2. 简谈 JavaScript、Java 中链式方法调用大致实现原理

    相信,在 JavaScript .C# 中都见过不少链式方法调用,那么,其中实现该类链式调用原理,大家有没有仔细思考过?其中 JavaScript 类库:jQuery 中就存在大量例子,而在 C# 中 ...

  3. Java链式方法 连贯接口(fluent interface)

    有两种情况可运用链式方法: 第一种  除最后一个方法外,每个方法都返回一个对象 object2 = object1.method1(); object3 = object2.method2(); ob ...

  4. C#用链式方法表达循环嵌套

    情节故事得有情节,不喜欢情节的朋友可看第1版代码,然后直接跳至“三.想要链式写法” 一.起缘 故事缘于一位朋友的一道题: 朋友四人玩LOL游戏.第一局,分别选择位置:中单,上单,ADC,辅助:第二局新 ...

  5. C#用链式方法

    C#用链式方法表达循环嵌套   情节故事得有情节,不喜欢情节的朋友可看第1版代码,然后直接跳至“三.想要链式写法” 一.起缘 故事缘于一位朋友的一道题: 朋友四人玩LOL游戏.第一局,分别选择位置:中 ...

  6. jQuery支持链式编程,一句话实现左侧table页+常用筛选器总结

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. JavaScript运动_封装模板(支持链式运动、完美运动)

    最近自学到了JS运动部分,自己整理了一些js模板,望采纳. 1.支持链式运动的模板: 先解释一下函数中的几个参数含义: 1)obj: 要操作的对象 2)target: 属性要到达的目标值 3)attr ...

  8. Java链式方法

    http://blog.csdn.net/lemon_shenzhen/article/details/6358537 有两种情况可运用链式方法: 第一种  除最后一个方法外,每个方法都返回一个对象 ...

  9. 自定义php-mysqli工具增强类,支持链式调用

    <?php /*数据库访问类,支持链式访问 *function table($table):表名 *function where($where):条件 *function field(...$f ...

随机推荐

  1. openstack cinder-volume 的高可用(HA)

    http://blog.csdn.net/LL_JCB/article/details/51879378 为了保证云平台的稳定性,需要做很多部分的高可用.比如控制节点高可用.计算节点高可用.网络节点高 ...

  2. poj3050

    #include <stdio.h> #include <set> #include <string> using namespace std; int a[6]; ...

  3. VS2005 “无法在证书存储区中找到清单签名证书”错误的解决方法

    方法一:在VS2005中出现该错误时,用记事本打开项目的.csproj文件,删除以下内容即可:    <ManifestCertificateThumbprint>B531F2CF2227 ...

  4. Linux部署apache

    一.我们使用源码安装 官网:https://httpd.apache.org/文档:https://httpd.apache.org/docs/2.4/ 下载源码包 httpd-2.4.20.tar. ...

  5. mfc 在VC的两个对话框类中传递参数的三种方法

    弄了好久,今天终于把在VC中的对话框类之间传递参数的问题解决了,很开心,记录如下: 1. 我所建立的工程是一个基于MFC对话框的应用程序,一共有三个对话框,第一个对话框为主对话框,所对应的类为CTMD ...

  6. [Leetcode][JAVA] Pascal's Triangle I, II

    Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...

  7. wav文件格式分析(一)

    (一)概述 WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(Resource Interchange File Format)文件规范: (二)音频知识 1.常见的声音文 ...

  8. 安装Tomcat服务器

    一.首先,下载Tomcat,你可以直接百度Tomcat官网, 或者,直接在地址栏输入他的官网地址:http://tomcat.apache.org/,然后进入他的主页,在主页左侧可以找到Downloa ...

  9. Sqoop-1.4.6.bin__hadoop-2.0.4-alpha 环境搭建

    一.Sqoop 环境搭建 1.下载安装包及解压     sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz 1)拷贝sqoop-1.4.6.bin__hadoop-2 ...

  10. kali linux 系列教程之metasploit 连接postgresql可能遇见的问题

    kali linux 系列教程之metasploit 连接postgresql可能遇见的问题 文/玄魂   目录 kali linux 下metasploit 连接postgresql可能遇见的问题. ...