麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

自用擴展方法分享

2019-11-17 03:52:09
字體:
來源:轉載
供稿:網友
 引言
自從用上擴展方法以來,就欲罷不能了,它們大大提升了我的代碼編寫效率,現在我已對其產生了高度依賴。在此分享一下自己的常用擴展方法集,方便大家使用。

(其中有些是借鑒或挪用自其它博友的文章,在此尤其感謝鶴沖天的諸多分享)

源代碼在文章末尾處提供。



示例


public static string ExpandAndToString(this System.Collections.IEnumerable s, string 間隔字符)

功能:將集合展開并分別執行ToString方法,再以指定的分隔符銜接,拼接成一個字符串。

范例:

[TestMethod]
public void TestMethod1()
{
     var i = new int[] {1,5,33,14,556 };
     var Out="1-5-33-14-556";
     Assert.AreEqual(Out,i.ExpandAndToString("-"));
}


public static bool IsNullOrEmpty(this string s)

功能:驗證字符串對象是否為空對象或空字符串。

范例:

[TestMethod]
public void TestMethod2()
{
    string s = null;
    Assert.AreEqual(true,s.IsNullOrEmpty());
    s += "123";
    Assert.AreEqual(false, s.IsNullOrEmpty());
}


public static string IsNullOrEmptyThen(this string s, System.Func<string,string> 表達式)

功能:驗證字符串對象是否為空對象或空字符串,如果是的話,則執行傳入表達式,并將表達式結果返回。

范例:

[TestMethod]
public void TestMethod3()
{
    var s = "";
    var Out = "1234";
    Assert.AreEqual(Out, s.IsNullOrEmptyThen(f=>"1234"));
}


public static void IsNullOrEmptyThen(this string s, System.Action<string> 表達式)

功能:驗證字符串對象是否為空對象或空字符串,如果是的話,則執行傳入表達式。

范例:

[TestMethod]
public void TestMethod4()
{
    var s = "";
    s.IsNullOrEmptyThen(f => MessageBox.Show("無內容"));
}


public static string FormatWith(this string s, params object[] 格式化參數)

public static string FormatWith(this string s, object 格式化參數1)

public static string FormatWith(this string s, object 格式化參數1, object 格式化參數2)

public static string FormatWith(this string s, object 格式化參數1, object 格式化參數2, object 格式化參數3)

功能:格式化字符串。

范例:

[TestMethod]
public void TestMethod5()
{
    var i = 0.35;
    var x = 200;
    var Out = "i:35%;x:200;";
    Assert.AreEqual(Out, "i:{0:0%};x:{1};".FormatWith(i,x));
}


public static bool In<T>(this T t, params T[] 判斷依據)

功能:判斷當前對象是否位于傳入數組中。

范例:

[TestMethod]
public void TestMethod6()
{
    var i = 95;
    Assert.IsTrue(i.In(31, 3, 55, 67, 95, 12, 4));
}


public static bool In<T, C>(this T t, System.Func<T,C,bool> 判斷表達式, params C[] 判斷依據)

功能:判斷當前對象是否位于傳入數組中,判斷方式由傳入表達式指定。

范例:

[TestMethod]
public void TestMethod7()
{
    var i = 95;
    Assert.IsTrue(i.In((c, t) => c.ToString() == t, "31", "3", "55", "67", "95", "12", "4"));
}


public static bool InRange<T>(this System.IComparable<T> t, T 最小值, T 最大值)

public static bool InRange(this System.IComparable t, object 最小值, object 最大值)

功能:判斷當前值是否介于指定范圍內。

范例:

[TestMethod]
public void TestMethod8()
{
    var i = 95;
    Assert.IsTrue(i.InRange(15, 100));
    Assert.IsTrue(i.InRange(-3000, 300));
    Assert.IsFalse(i.InRange(-1, 50));
    var s = "b";
    Assert.IsTrue(s.InRange("a", "c"));
    Assert.IsTrue(s.InRange("1", "z"));
    Assert.IsFalse(s.InRange("e", "h"));
}


public static T Trace<T>(this T t)

public static T Trace<T>(this T t, string 分類)

public static T Trace<T>(this T t, System.Func<T,object> 表達式)

public static T Trace<T>(this T t, System.Func<T,object> 表達式, string 分類)

功能:將當前對象的值輸出到Visual Studio輸出窗口中,并將原始對象返回。此功能僅用于方便調試,可以在方法鏈中的任意步驟中輸出值,而不會對方法鏈的連續性有任何影響。

范例:

[TestMethod]
public void TestMethod9()
{
    var s = "abcdefg".Trace(f => f.ToUpper(), "表達式模式").Remove(4).Trace("普通模式");
    var Out = "abcd";
    Assert.AreEqual(Out, s);
    //輸出內容如下:
    //表達式模式: ABCDEFG
    //普通模式: abcd
}


public static T TraceFormat<T>(this T t, string 格式化字符串)

public static T TraceFormat<T>(this T t, string 格式化字符串, string 分類)

功能:將當前對象的值經格式化后輸出到VisualStudio輸出窗口中,并將原始對象返回。此功能僅用于方便調試,可以在方法鏈中的任意步驟中輸出值,而不會對方法鏈的連續性有任何影響。

范例:

[TestMethod]
public void TestMethod10()
{
    var m = Math.Max(0.31, 0.65).TraceFormat("Max Value Is {0}", "格式化模式");
    var Out = 0.65;
    Assert.AreEqual(Out, m);
    //輸出內容如下:
    //格式化模式: Max Value Is 0.65
}


public static void ForEach<T>(this System.Collections.Generic.IEnumerable<T> source, System.Action<T> 操作)

public static void ForEach<T>(this System.Collections.Generic.IEnumerable<T> source, System.Action<T,int> 操作)

功能:遍歷一個集合,執行指定操作。(重載形式中,傳入表達式的int類型參數代表當前循環次數)

范例:

[TestMethod]
public void TestMethod11()
{
    var l = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    var c = 0;
    l.ForEach(f => c += f);
    var Out = 45;
    Assert.AreEqual(Out, c);
    l.ForEach((f, i) => c -= i);
    Out = 9;
    Assert.AreEqual(Out, c);
}


public static Switch<T> Switch<T>(this T v)

public static Case<T,R> Switch<T, R>(this T v, System.Func<R,R,R> Do)

功能:判斷當前值,根據不同匹配條件執行相應操作或返回相應的值。(重載形式中,允許通過表達式對每一次的返回值進行疊加處理)
詳細使用說明參看:《稍加改進的Switch/Case擴展方法》

范例:

[TestMethod]
public void TestMethod12()
{
    var i = 15;
    i.Switch()
        .CaseRun(15, f => MessageBox.Show("等于15"),false)
        .CaseRun(f => f > 0, f => MessageBox.Show("大于0"))
        .CaseRun(f => f < 0, f => MessageBox.Show("小于0"))
        .DefaultRun(f => MessageBox.Show("等于0"));
    var o = 'c'.Switch()
        .CaseReturn('a', 1)
        .CaseReturn('b', 2)
        .CaseReturn('c', 3)
        .CaseReturn('d', 4)
        .CaseReturn(f => f > 'd', 5)
        .DefaultReturn(0).ReturnValue;
    Assert.AreEqual(3, o);
}


public static System.Collections.Generic.IEnumerable<T> RecursionSelect<T>(this T o, System.Func<T,IEnumerable<T>> 遞歸項選取表達式)

public static System.Collections.Generic.IEnumerable<T> RecursionSelect<T>(this T o, System.Func<T,IEnumerable<T>> 遞歸項選取表達式, System.PRedicate<T> 檢驗表達式)

功能:遞歸選取項目,并將最終選定的集合返回。
相關原理說明參看:《c#擴展方法奇思妙用高級篇七:“樹”通用遍歷器》

范例:

[TestMethod]
public void TestMethod13()
{
    //獲取指定目錄中所有包含子目錄的目錄集合
    var d = new DirectoryInfo(@"C:/Users/Public/Downloads");
    var c = d.RecursionSelect(f => f.GetDirectories(), f => f.GetDirectories().Length > 0);
    MessageBox.Show(c.Count().ToString());
}


public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.IEnumerable o, System.Func<T,IEnumerable<T>> 遞歸項選取表達式)

public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.IEnumerable o, System.Func<T,IEnumerable<T>> 遞歸項選取表達式, System.Predicate<T> 檢驗表達式)

public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.Generic.IEnumerable<T> o, System.Func<T,IEnumerable<T>> 遞歸項選取表達式)

public static System.Collections.Generic.IEnumerable<T> RecursionEachSelect<T>(this System.Collections.Generic.IEnumerable<T> o, System.Func<T,IEnumerable<T>> 遞歸項選取表達式, System.Predicate<T> 檢驗表達式)

功能:遍歷當前集合對象,逐一遞歸選取項目,并將最終選定的集合返回。
相關原理說明參看:《c#擴展方法奇思妙用高級篇七:“樹”通用遍歷器》

范例:

[TestMethod]
public void TestMethod14()
{
    //獲取指定目錄中所有包含子目錄的目錄集合
    var l = new List<DirectoryInfo>();
    l.Add(new DirectoryInfo(@"C:/Users/SkyD/Downloads"));
    l.Add(new DirectoryInfo(@"C:/Users/Public/Downloads"));            
    var c = l.RecursionEachSelect(f => f.GetDirectories(), f => f.GetDirectories().Length > 0);
    MessageBox.Show(c.Count().ToString());
}


public static bool RegexIsMatch(this string s, string 表達式, System.Text.RegularExpressions.RegexOptions 選項)

public static bool RegexIsMatch(this string s, string 表達式)

public static System.Text.RegularExpressions.Match RegexMatch(this string s, string 表達式, System.Text.RegularExpressions.RegexOptions 選項)

public static System.Text.RegularExpressions.Match RegexMatch(this string s, string 表達式)

public static System.Text.RegularExpressions.MatchCollection RegexMatches(this string s, string 表達式, System.Text.RegularExpressions.RegexOptions 選項)

public static System.Text.RegularExpressions.MatchCollection RegexMatches(this string s, string 表達式)

public static string RegexReplace(this string s, string 表達式, string 替換值, System.Text.RegularExpressions.RegexOptions 選項)

public static string RegexReplace(this string s, string 表達式, string 替換值)

public static string[] RegexSplit(this string s, string 表達式, System.Text.RegularExpressions.RegexOptions 選項)

public static string[] RegexSplit(this string s, string 表達式)

功能:常用正則表達式功能封裝,使用方法與Regex類相同。



public static T As<T>(this string s) where T : new(), 通用擴展.SpecialString

public static 通用擴展.HtmlString AsHtmlString(this string s)

public static 通用擴展.PathString aspathString(this string s)

public static 通用擴展.ServerPathString AsServerPathString(this string s)

public static 通用擴展.UriString AsUriString(this string s)

public static 通用擴展.XHtmlString AsXHtmlString(this string s)

public static 通用擴展.xmlString AsXmlString(this string s)

功能:定義為特殊類型的字符串,以使用特有的格式化命令做進一步修改。(目前定義后的后續格式化功能比較有限,以后會逐步追加)

范例:

[TestMethod]
public void TestMethod15()
{
    var s = @"C:/abc/";
    var Out = @"C:/abc/1.exe";
    Assert.AreEqual(Out, s.AsPathString().Combine(@"D:/1.exe".AsPathString().FileName));
}


結語
這些都是我這里使用頻率最高的擴展,希望對大家也同樣有用:)



下載
擴展方法源代碼:http://www.uushare.com/user/icesee/file/2435046

范例源代碼:http://www.uushare.com/user/icesee/file/2435063

本文的XPS版本:http://www.uushare.com/user/icesee/file/2435098


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91精品老司机 | 午夜在线视频一区二区三区 | 国产午夜精品一区二区三区不卡 | 狠狠干狠狠操 | 黄视频网站免费在线观看 | 国产欧美在线一区二区三区 | 日韩一级片毛片 | 19禁国产精品福利视频 | 久产久精品 | 欧美一级免费在线观看 | 高清做爰免费无遮网站挡 | 国产精品午夜未成人免费观看 | 精品国产一区二区三区四区阿崩 | 国产91久久精品一区二区 | 成人在线观看一区二区 | 欧美日韩国产成人在线 | 成人福利在线视频 | 涩涩伊人 | 欧美色另类 | 亚洲一区二区在线视频 | 亚洲射吧 | 亚洲福利视| 国产午夜精品一区 | 久久精品亚洲一区二区 | 九九热精品在线 | 黄wwww| 中文字幕 在线观看 | 国人精品视频在线观看 | 91羞羞| 久久99精品国产99久久6男男 | h色网站在线观看 | 日本成人在线播放 | 久久国产精品小视频 | 国产精品无码久久久久 | 黄色大片高清 | 狠狠干狠狠操 | 成人性生活视频在线播放 | 性少妇freeseⅹbbwhd | 欧美激情 在线播放 | 欧美在线观看视频网站 | 成人激情综合网 |