最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

ASP技自用扩展方法巧分享

日期:02-09    来源:中国设计秀    作者:cnwebshow.com

 引言psf中国设计秀
自从用上扩展方法以来,就欲罢不能了,它们大大提升了我的代码编写效率,现在我已对其产生了高度依赖。在此分享一下自己的常用扩展方法集,方便大家使用。psf中国设计秀
psf中国设计秀
(其中有些是借鉴或挪用自其它博友的文章,在此尤其感谢鹤冲天的诸多分享)psf中国设计秀
psf中国设计秀
源代码在文章末尾处提供。psf中国设计秀
psf中国设计秀
psf中国设计秀
psf中国设计秀
示例psf中国设计秀
psf中国设计秀
psf中国设计秀
public static string ExpandAndToString(this System.Collections.IEnumerable s, string 间隔字符) psf中国设计秀
psf中国设计秀
功能:将集合展开并分别执行ToString方法,再以指定的分隔符衔接,拼接成一个字符串。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod1()psf中国设计秀
{psf中国设计秀
     var i = new int[] {1,5,33,14,556 };psf中国设计秀
     var Out="1-5-33-14-556";psf中国设计秀
     Assert.AreEqual(Out,i.ExpandAndToString("-"));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static bool IsNullOrEmpty(this string s)psf中国设计秀
psf中国设计秀
功能:验证字符串对象是否为空对象或空字符串。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod2()psf中国设计秀
{psf中国设计秀
    string s = null;psf中国设计秀
    Assert.AreEqual(true,s.IsNullOrEmpty());psf中国设计秀
    s += "123";psf中国设计秀
    Assert.AreEqual(false, s.IsNullOrEmpty());psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static string IsNullOrEmptyThen(this string s, System.Func<string,string> 表达式)psf中国设计秀
psf中国设计秀
功能:验证字符串对象是否为空对象或空字符串,如果是的话,则执行传入表达式,并将表达式结果返回。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod3()psf中国设计秀
{psf中国设计秀
    var s = "";psf中国设计秀
    var Out = "1234";psf中国设计秀
    Assert.AreEqual(Out, s.IsNullOrEmptyThen(f=>"1234"));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static void IsNullOrEmptyThen(this string s, System.Action<string> 表达式)psf中国设计秀
psf中国设计秀
功能:验证字符串对象是否为空对象或空字符串,如果是的话,则执行传入表达式。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod4()psf中国设计秀
{psf中国设计秀
    var s = "";psf中国设计秀
    s.IsNullOrEmptyThen(f => MessageBox.Show("无内容"));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static string FormatWith(this string s, params object[] 格式化参数)psf中国设计秀
psf中国设计秀
public static string FormatWith(this string s, object 格式化参数1)psf中国设计秀
psf中国设计秀
public static string FormatWith(this string s, object 格式化参数1, object 格式化参数2)psf中国设计秀
psf中国设计秀
public static string FormatWith(this string s, object 格式化参数1, object 格式化参数2, object 格式化参数3)psf中国设计秀
psf中国设计秀
功能:格式化字符串。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod5()psf中国设计秀
{psf中国设计秀
    var i = 0.35;psf中国设计秀
    var x = 200;psf中国设计秀
    var Out = "i:35%;x:200;";psf中国设计秀
    Assert.AreEqual(Out, "i:{0:0%};x:{1};".FormatWith(i,x));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static bool In<T>(this T t, params T[] 判断依据)psf中国设计秀
psf中国设计秀
功能:判断当前对象是否位于传入数组中。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod6()psf中国设计秀
{psf中国设计秀
    var i = 95;psf中国设计秀
    Assert.IsTrue(i.In(31, 3, 55, 67, 95, 12, 4));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static bool In<T, C>(this T t, System.Func<T,C,bool> 判断表达式, params C[] 判断依据)psf中国设计秀
psf中国设计秀
功能:判断当前对象是否位于传入数组中,判断方式由传入表达式指定。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod7()psf中国设计秀
{psf中国设计秀
    var i = 95;psf中国设计秀
    Assert.IsTrue(i.In((c, t) => c.ToString() == t, "31", "3", "55", "67", "95", "12", "4"));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static bool InRange<T>(this System.IComparable<T> t, T 最小值, T 最大值)psf中国设计秀
psf中国设计秀
public static bool InRange(this System.IComparable t, object 最小值, object 最大值)psf中国设计秀
psf中国设计秀
功能:判断当前值是否介于指定范围内。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod8()psf中国设计秀
{psf中国设计秀
    var i = 95;psf中国设计秀
    Assert.IsTrue(i.InRange(15, 100));psf中国设计秀
    Assert.IsTrue(i.InRange(-3000, 300));psf中国设计秀
    Assert.IsFalse(i.InRange(-1, 50));psf中国设计秀
    var s = "b";psf中国设计秀
    Assert.IsTrue(s.InRange("a", "c"));psf中国设计秀
    Assert.IsTrue(s.InRange("1", "z"));psf中国设计秀
    Assert.IsFalse(s.InRange("e", "h"));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static T Trace<T>(this T t)psf中国设计秀
psf中国设计秀
public static T Trace<T>(this T t, string 分类)psf中国设计秀
psf中国设计秀
public static T Trace<T>(this T t, System.Func<T,object> 表达式)psf中国设计秀
psf中国设计秀
public static T Trace<T>(this T t, System.Func<T,object> 表达式, string 分类)psf中国设计秀
psf中国设计秀
功能:将当前对象的值输出到Visual Studio输出窗口中,并将原始对象返回。此功能仅用于方便调试,可以在方法链中的任意步骤中输出值,而不会对方法链的连续性有任何影响。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod9()psf中国设计秀
{psf中国设计秀
    var s = "abcdefg".Trace(f => f.ToUpper(), "表达式模式").Remove(4).Trace("普通模式");psf中国设计秀
    var Out = "abcd";psf中国设计秀
    Assert.AreEqual(Out, s);psf中国设计秀
    //输出内容如下:psf中国设计秀
    //表达式模式: ABCDEFGpsf中国设计秀
    //普通模式: abcdpsf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static T TraceFormat<T>(this T t, string 格式化字符串)psf中国设计秀
psf中国设计秀
public static T TraceFormat<T>(this T t, string 格式化字符串, string 分类)psf中国设计秀
psf中国设计秀
功能:将当前对象的值经格式化后输出到VisualStudio输出窗口中,并将原始对象返回。此功能仅用于方便调试,可以在方法链中的任意步骤中输出值,而不会对方法链的连续性有任何影响。psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod10()psf中国设计秀
{psf中国设计秀
    var m = Math.Max(0.31, 0.65).TraceFormat("Max Value Is {0}", "格式化模式");psf中国设计秀
    var Out = 0.65;psf中国设计秀
    Assert.AreEqual(Out, m);psf中国设计秀
    //输出内容如下:psf中国设计秀
    //格式化模式: Max Value Is 0.65psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static void ForEach<T>(this System.Collections.Generic.IEnumerable<T> source, System.Action<T> 操作)psf中国设计秀
psf中国设计秀
public static void ForEach<T>(this System.Collections.Generic.IEnumerable<T> source, System.Action<T,int> 操作)psf中国设计秀
psf中国设计秀
功能:遍历一个集合,执行指定操作。(重载形式中,传入表达式的int类型参数代表当前循环次数)psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod11()psf中国设计秀
{psf中国设计秀
    var l = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };psf中国设计秀
    var c = 0;psf中国设计秀
    l.ForEach(f => c += f);psf中国设计秀
    var Out = 45;psf中国设计秀
    Assert.AreEqual(Out, c);psf中国设计秀
    l.ForEach((f, i) => c -= i);psf中国设计秀
    Out = 9;psf中国设计秀
    Assert.AreEqual(Out, c);psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static Switch<T> Switch<T>(this T v)psf中国设计秀
psf中国设计秀
public static Case<T,R> Switch<T, R>(this T v, System.Func<R,R,R> Do)psf中国设计秀
psf中国设计秀
功能:判断当前值,根据不同匹配条件执行相应操作或返回相应的值。(重载形式中,允许通过表达式对每一次的返回值进行叠加处理)psf中国设计秀
详细使用说明参看:《稍加改进的Switch/Case扩展方法》psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod12()psf中国设计秀
{psf中国设计秀
    var i = 15;psf中国设计秀
    i.Switch()psf中国设计秀
        .CaseRun(15, f => MessageBox.Show("等于15"),false)psf中国设计秀
        .CaseRun(f => f > 0, f => MessageBox.Show("大于0"))psf中国设计秀
        .CaseRun(f => f < 0, f => MessageBox.Show("小于0"))psf中国设计秀
        .DefaultRun(f => MessageBox.Show("等于0"));psf中国设计秀
    var o = 'c'.Switch()psf中国设计秀
        .CaseReturn('a', 1)psf中国设计秀
        .CaseReturn('b', 2)psf中国设计秀
        .CaseReturn('c', 3)psf中国设计秀
        .CaseReturn('d', 4)psf中国设计秀
        .CaseReturn(f => f > 'd', 5)psf中国设计秀
        .DefaultReturn(0).ReturnValue;psf中国设计秀
    Assert.AreEqual(3, o);psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static System.Collections.Generic.IEnumerable<T> RecursionSelect<T>(this T o, System.Func<T,IEnumerable<T>> 递归项选取表达式)psf中国设计秀
psf中国设计秀
public static System.Collections.Generic.IEnumerable<T> RecursionSelect<T>(this T o, System.Func<T,IEnumerable<T>> 递归项选取表达式, System.PRedicate<T> 检验表达式)psf中国设计秀
psf中国设计秀
功能:递归选取项目,并将最终选定的集合返回。psf中国设计秀
相关原理说明参看:《c#扩展方法奇思妙用高级篇七:“树”通用遍历器》psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod13()psf中国设计秀
{psf中国设计秀
    //获取指定目录中所有包含子目录的目录集合psf中国设计秀
    var d = new DirectoryInfo(@"C:UsersPublicDownloads");psf中国设计秀
    var c = d.RecursionSelect(f => f.GetDirectories(), f => f.GetDirectories().Length > 0);psf中国设计秀
    MessageBox.Show(c.Count().ToString());psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.IEnumerable o, System.Func<T,IEnumerable<T>> 递归项选取表达式)psf中国设计秀
psf中国设计秀
public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.IEnumerable o, System.Func<T,IEnumerable<T>> 递归项选取表达式, System.Predicate<T> 检验表达式)psf中国设计秀
psf中国设计秀
public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.Generic.IEnumerable<T> o, System.Func<T,IEnumerable<T>> 递归项选取表达式)psf中国设计秀
psf中国设计秀
public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.Generic.IEnumerable<T> o, System.Func<T,IEnumerable<T>> 递归项选取表达式, System.Predicate<T> 检验表达式)psf中国设计秀
psf中国设计秀
功能:遍历当前集合对象,逐一递归选取项目,并将最终选定的集合返回。psf中国设计秀
相关原理说明参看:《c#扩展方法奇思妙用高级篇七:“树”通用遍历器》psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod14()psf中国设计秀
{psf中国设计秀
    //获取指定目录中所有包含子目录的目录集合psf中国设计秀
    var l = new List<DirectoryInfo>();psf中国设计秀
    l.Add(new DirectoryInfo(@"C:UsersSkyDDownloads"));psf中国设计秀
    l.Add(new DirectoryInfo(@"C:UsersPublicDownloads"));            psf中国设计秀
    var c = l.RecursionEachSelect(f => f.GetDirectories(), f => f.GetDirectories().Length > 0);psf中国设计秀
    MessageBox.Show(c.Count().ToString());psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
public static bool RegexIsMatch(this string s, string 表达式, System.Text.RegularExpressions.RegexOptions 选项)psf中国设计秀
psf中国设计秀
public static bool RegexIsMatch(this string s, string 表达式)psf中国设计秀
psf中国设计秀
public static System.Text.RegularExpressions.Match RegexMatch(this string s, string 表达式, System.Text.RegularExpressions.RegexOptions 选项)psf中国设计秀
psf中国设计秀
public static System.Text.RegularExpressions.Match RegexMatch(this string s, string 表达式)psf中国设计秀
psf中国设计秀
public static System.Text.RegularExpressions.MatchCollection RegexMatches(this string s, string 表达式, System.Text.RegularExpressions.RegexOptions 选项)psf中国设计秀
psf中国设计秀
public static System.Text.RegularExpressions.MatchCollection RegexMatches(this string s, string 表达式)psf中国设计秀
psf中国设计秀
public static string RegexReplace(this string s, string 表达式, string 替换值, System.Text.RegularExpressions.RegexOptions 选项)psf中国设计秀
psf中国设计秀
public static string RegexReplace(this string s, string 表达式, string 替换值)psf中国设计秀
psf中国设计秀
public static string[] RegexSplit(this string s, string 表达式, System.Text.RegularExpressions.RegexOptions 选项)psf中国设计秀
psf中国设计秀
public static string[] RegexSplit(this string s, string 表达式)psf中国设计秀
psf中国设计秀
功能:常用正则表达式功能封装,使用方法与Regex类相同。psf中国设计秀
psf中国设计秀
psf中国设计秀
psf中国设计秀
public static T As<T>(this string s) where T : new(), 通用扩展.SpecialStringpsf中国设计秀
psf中国设计秀
public static 通用扩展.HtmlString AsHtmlString(this string s)psf中国设计秀
psf中国设计秀
public static 通用扩展.PathString aspathString(this string s)psf中国设计秀
psf中国设计秀
public static 通用扩展.ServerPathString AsServerPathString(this string s)psf中国设计秀
psf中国设计秀
public static 通用扩展.UriString AsUriString(this string s)psf中国设计秀
psf中国设计秀
public static 通用扩展.XHtmlString AsXHtmlString(this string s)psf中国设计秀
psf中国设计秀
public static 通用扩展.xmlString AsXmlString(this string s)psf中国设计秀
psf中国设计秀
功能:定义为特殊类型的字符串,以使用特有的格式化命令做进一步修改。(目前定义后的后续格式化功能比较有限,以后会逐步追加)psf中国设计秀
psf中国设计秀
范例:psf中国设计秀
psf中国设计秀
[TestMethod]psf中国设计秀
public void TestMethod15()psf中国设计秀
{psf中国设计秀
    var s = @"C:abc";psf中国设计秀
    var Out = @"C:abc1.exe";psf中国设计秀
    Assert.AreEqual(Out, s.AsPathString().Combine(@"D:1.exe".AsPathString().FileName));psf中国设计秀
}psf中国设计秀
psf中国设计秀
psf中国设计秀
结语psf中国设计秀
这些都是我这里使用频率最高的扩展,希望对大家也同样有用:)psf中国设计秀
psf中国设计秀
psf中国设计秀
psf中国设计秀
下载psf中国设计秀
扩展方法源代码:http://www.uushare.com/user/icesee/file/2435046psf中国设计秀
psf中国设计秀
范例源代码:http://www.uushare.com/user/icesee/file/2435063psf中国设计秀
psf中国设计秀
本文的XPS版本:http://www.uushare.com/user/icesee/file/2435098psf中国设计秀
psf中国设计秀
 psf中国设计秀

本文引用地址:/bc/article_46122.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明