[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 分类)
[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 分类)
[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)
[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> 检验表达式)
[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> 检验表达式)
[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 表达式)
[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)); }