本篇体验扩展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. Winform中DockPanel(引用WeifenLuo.WinFormsUI.Docking.dll组件)的使用

    WeiFenLuo.WinFormsUI.Docking.dll是开源项目DockPanel Suite的一个类库,可实现像Visual Studio的窗口停靠.拖拽等功能.WeifenLuo.Win ...

  2. CentOS6.4完全安装FFmpeg手记

    鼓捣媒体的人对FFmpeg应该不会陌生,它不仅功能强大,结构优美,灵活.易扩展,也是很其他多媒体播放器的基础,例如VLC,Mplayer等等,还有好多商业播放器都用了ffmpeg,但这些商业软件却没有 ...

  3. git使用2

    初始化配置 #配置使用git仓库的人员姓名 git config --global user.name "Your Name Comes Here" #配置使用git仓库的人员em ...

  4. Daily Scrum 12.1

    今日完成任务: 完成了对源代码结构的修改,删除冗余等:和其他小组讨论了关于整合的问题,向其他小组介绍自己小组使用的数据库等. 明日任务: 晏旭瑞 初步完成文档上传下载 孙思权 深入了解数据库中每个表, ...

  5. 0729am空控制器

  6. 贪心+构造( Codeforces Round #344 (Div. 2))

    题目:Report 题意:有两种操作: 1)t = 1,前r个数字按升序排列:   2)t = 2,前r个数字按降序排列: 求执行m次操作后的排列顺序. #include <iostream&g ...

  7. Silverlight 4常用StringFormat格式总结

    原文地址:http://www.silverlightchina.net/html/tips/2011/0424/7149.html 在Silverlight项目中,经常会根据需求的不同,提供不同的字 ...

  8. Fragment的基本用法

    一.Fragment主要用到的API: 1.Fragment 类-----用来创建碎片 2.FragmentManager 类 ----为管理Activity中Fragment,用于Activity与 ...

  9. [Leetcode][JAVA] Word Ladder

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  10. FFMpeg写MP4文件例子分析

    http://blog.csdn.net/eightdegree/article/details/7425811 这段时间看了FFMpeg提供的例子muxing.c,我略微修改了下源代码,使其生成一个 ...