天天看點

穿着馬甲的編碼方式----鍊式

 哈~   

上代碼:

  1 using Microsoft.VisualStudio.TestTools.UnitTesting;

  2 using System.Diagnostics;

  3 using System;

  4 

  5 namespace Demo.TestProject

  6 {

  7     /// <summary>

  8     /// 鍊式編碼Demo

  9     /// </summary>

 10     [TestClass]

 11     public class UnitTest

 12     {

 13 

 14         [TestMethod]

 15         public void TestMethod1()

 16         {

 17             string hello = "Hello ";

 18             string world = "World";

 19 

 20             try

 21             {  

 22                 //例1: 有傳回值(傳回本身)的例子

 23                 new TestClasslmp { UName = "HLS" }

 24                     .StrStart()

 25                     .StrAppend(hello, world)

 26                     .StrEnd();

 27                 //例2: 無傳回值的例子

 28                 new TestClasslmp2 { UName = "HLSTwo" }

 29                     .Do<TestClasslmp2>(p => p.StrStart())

 30                     .Do(p => p.StrAppend(hello, world))

 31                     .Do(p => p.StrEnd());

 32 

 33             }

 34             catch (Exception e)

 35             {

 36                 throw new Exception("Error:" + e.Message);

 37             }

 38 

 39 

 40         }

 41     }

 42 

 43     /*****例1: 有傳回值(傳回本身)的例子*****/

 44     public interface ITestClass

 45     {

 46         ITestClass StrStart();

 47         ITestClass StrAppend(string hello, string world);

 48         ITestClass StrEnd();

 49     }

 50 

 51     public class TestClasslmp : ITestClass

 52     {

 53         public string UName { get; set; }

 54         #region ITestClass 成員

 55 

 56         public ITestClass StrStart()

 57         {

 58             Trace.WriteLine("開始輸出...");

 59             return this;

 60         }

 61 

 62         public ITestClass StrAppend(string hello, string world)

 63         {

 64             Trace.WriteLine("輸出: " + this.UName + " Say: " + hello + " " + world + ".");

 65             return this;

 66         }

 67 

 68         public ITestClass StrEnd()

 69         {

 70             Trace.WriteLine("完成!");

 71             return this;

 72         }

 73 

 74         #endregion

 75     }

 76 

 77 

 78     /*****例2: 無傳回值(傳回本身)的例子*****/

 79     public interface ITestClass2

 80     {

 81         void StrStart();

 82         void StrAppend(string hello, string world);

 83         void StrEnd();

 84     }

 85 

 86     public class TestClasslmp2 : ITestClass2

 87     {

 88         public string UName { get; set; }

 89         #region ITestClass 成員

 90 

 91         public void StrStart()

 92         {

 93             Trace.WriteLine("開始輸出...");

 94         }

 95 

 96         public void StrAppend(string hello, string world)

 97         {

 98             Trace.WriteLine("輸出: " + this.UName + " Say: " + hello + " " + world + ".");

 99         }

100 

101         public void StrEnd()

102         {

103             Trace.WriteLine("完成!");

104         }

105 

106         #endregion

107     }

108 

109    /*****定義一個處理了鍊式void的方法*****/

110     public static class MyClass

111     {

112         public static T Do<T>(this T t, Action<T> action)

113         {

114             action(t);

115             return t;

116         }

117     }

118 }

繼續閱讀