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("\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支持链式方法的更多相关文章
- .NetCore 中扩展ExceptionLess 实现链式方法添加操作日志
在使用ExceptionLess添加日志的时候,发现还是有一些写法上的个人觉得不爽的地方,比如添加Info日志 ExceptionlessClient.Default.CreateLog(source ...
- 简谈 JavaScript、Java 中链式方法调用大致实现原理
相信,在 JavaScript .C# 中都见过不少链式方法调用,那么,其中实现该类链式调用原理,大家有没有仔细思考过?其中 JavaScript 类库:jQuery 中就存在大量例子,而在 C# 中 ...
- Java链式方法 连贯接口(fluent interface)
有两种情况可运用链式方法: 第一种 除最后一个方法外,每个方法都返回一个对象 object2 = object1.method1(); object3 = object2.method2(); ob ...
- C#用链式方法表达循环嵌套
情节故事得有情节,不喜欢情节的朋友可看第1版代码,然后直接跳至“三.想要链式写法” 一.起缘 故事缘于一位朋友的一道题: 朋友四人玩LOL游戏.第一局,分别选择位置:中单,上单,ADC,辅助:第二局新 ...
- C#用链式方法
C#用链式方法表达循环嵌套 情节故事得有情节,不喜欢情节的朋友可看第1版代码,然后直接跳至“三.想要链式写法” 一.起缘 故事缘于一位朋友的一道题: 朋友四人玩LOL游戏.第一局,分别选择位置:中 ...
- jQuery支持链式编程,一句话实现左侧table页+常用筛选器总结
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- JavaScript运动_封装模板(支持链式运动、完美运动)
最近自学到了JS运动部分,自己整理了一些js模板,望采纳. 1.支持链式运动的模板: 先解释一下函数中的几个参数含义: 1)obj: 要操作的对象 2)target: 属性要到达的目标值 3)attr ...
- Java链式方法
http://blog.csdn.net/lemon_shenzhen/article/details/6358537 有两种情况可运用链式方法: 第一种 除最后一个方法外,每个方法都返回一个对象 ...
- 自定义php-mysqli工具增强类,支持链式调用
<?php /*数据库访问类,支持链式访问 *function table($table):表名 *function where($where):条件 *function field(...$f ...
随机推荐
- Android.技术站点
总结Android相关的技术站点和blog 1. http://android-developers.blogspot.com/ 首推这个blog,有时间需要每篇blog读一遍. 2. nlopez ...
- Ceph分层存储分析
最近弄Ceph集群考虑要不要加入分层存储 因此花了点时间研究了下 1,首先肯定要弄清Ceph分层存储的结构 ,结构图大概就是下图所示 缓存层(A cache tier)为Ceph客户端提供更好的I/O ...
- Ubuntu10.0.4安装NDK
android版本遇到.so文件crash,需要使用ndk来定位报错代码. 从这里下载ndk安装文件: http://www.androiddevtools.cn/ 运行 ./android-ndk- ...
- freeCodeCamp:Title Case a Sentence
确保字符串的每个单词首字母都大写,其余部分小写. 像'the'和'of'这样的连接符同理. /*思路 将字符串转为小写.toLowerCase() 分割字符串以单词形式组成数组myarr 确保数组中的 ...
- Acunetix Web漏洞扫描器
1.主要程序介绍 主要操作区域简介: b).工具栏 从左到右分别是(这些都可以在主要操作区域找到,所以不常用): 新建扫描——网站扫描——网站爬行——目标查找——目标探测——子域名扫描——SQL盲注— ...
- [转载]理解HTML语义化
声明: 本文转载于:freeyiyi1993博客. 原文地址:http://www.cnblogs.com/freeyiyi1993/p/3615179.html 1.什么是HTML语义化? < ...
- Storm启动流程简介
storm启动流程 storm是一个流行的开源的,分布式实时处理框架,关于storm的基本介绍可以参加这篇官方文档.大致的拓扑结构如图所示: 其中Nimbus是一个后台 ...
- javascript基础知识-对象
javascript创建对象有三种方法: 1)对象直接量 例:var empty = {}; var point = {x:1,y:4}; var book = { "main title& ...
- Cocos2d-x Application Wizard for Visual Studio User Guide
0. Overview Cocos2d-x-win32's project can be generated by Wizard. Wizard supports Visual Studio 2008 ...
- 第六天:用javascript实现购彩拆分票的计算奖金
需求如下: 购彩金额 拆分票数 <= 10 1票<= 100 10票<= 200 20票<= 500 50票<= 1000 100票 中奖金额 ...