穿着马甲的编码方式链式

我们要做实干家,不做意淫者。哈~   

上代码:

  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 { getset; }
 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 { getset; }
 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 }
原文地址:https://www.cnblogs.com/haoliansheng/p/1762448.html