C#中扩展StringBuilder支持链式方法
2016-03-13 12:49
603 查看
本篇体验扩展StringBuilder使之支持链式方法。
这里有一个根据键值集合生成select元素的方法。
以上,
html.AppendFormat("<select id=\"{0}\" name=\"{0}\">", id);
html.AppendLine();
可以对这两个语句封装,扩展StringBuilder。
改成
以上,
if(includeUnknown)
{
html.AppendLine("\t<option>Unknown</option>");
}
可以对如上语句进行封装,继续扩展StringBuilder.
或
以上,
foreach(var opt in options)
{
html.AppendFormattedLine("\t<option value=\"{0}\">{1}</option>", opt.Key, opt.Value);
}
对遍历语句进行封装,扩展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(); }
相关文章推荐
- 02-线性结构3 Pop Sequence
- 使用LinkedList实现Stack与Queue
- UINavigationController, UITabBarController
- android 6.0 SystemUI源码分析(6)-StorageNotification介绍
- com.mchange.v2.resourcepool.CannotAcquireResourceException
- com.mchange.v2.resourcepool.CannotAcquireResourceException
- (4.2.25)22个值得收藏的Android开源代码-UI篇
- 关于Android的Build类——获取Android手机设备各种信息
- UIScrollView、UIPageControl、NSTimer
- JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedur
- 通过代理更新UITableHeaderFooterView某一行数据时,如何获取更新哪一行的
- C#中String与StringBuilder的区别
- 设置为我们的UITableViewFooterHeaderView组标题的高度
- 为我们的UITableViewFooterHeaderView添加自定义View的布局
- 为我们的UITableViewFooterHeaderView设置某一行或所有的行高度
- 设置UITableViewHeaderFooterView的背景颜色
- 自定义UITableViewFooterHeaderView时调用自身的self.bounds为0的情况
- UITableViewHeaderFooterView当中的方法
- android studio authentication required解决方案
- PB 导入和导出Excel