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 ...
随机推荐
- linux 下两台电脑之间ssh无密码连接
例子:在192.168.0.12使用tecmint用户,连接192.168.0.11主机上的sheena用户 Step 1: Create Authentication SSH-Kegen Keys ...
- mysql 日志
1.error_log 记录mysql的启动关闭的信息 记录mysql服务器运行错误的信息 记录mysql的表检查或修复信息 路径:my.cnf中通过--log-error=[file_name]配置 ...
- c#开发Mongo笔记第七篇
开发到这里遇到了一些问题,哪到这里想请教一下大家 今天我完成的是菜单功能, public class Menu { public ObjectId _id { get; set; } public i ...
- 20145225唐振远 实验二 "Java面向对象程序设计"
20145225<Java程序设计> 实验二 Java面向对象程序设计 实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S ...
- Leveldb之version与version_set详细对比
version类包含的重要变量: VersionSet* vset_; // VersionSet to which this Version belongs Version* next_; // N ...
- 拾遗:『Linux Capability』
『Linux Capability』 For the purpose of performing permission checks, traditional UNIX implementations ...
- SSH Secure Shell Client中文乱码的解决办法
#vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030" LANGUAGE="zh_CN.GB18030:zh_CN. ...
- I hate it
Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老 ...
- ubuntu下phpmyadmin配置
经常出现的问题就是明明安装了phpmyadmin但却在输入 http://localhost/phpmyadmin的时候,没有出现管理界面,反而出现没有找到的页面. 不急,我们先安装再了phpmyad ...
- iOS 设置navigationBar背景
- (void)viewWillAppear:(BOOL)animated { [superviewWillAppear:animated]; [self.navigationContro ...