匿名事件類別

來源:互聯網
上載者:User

標籤:

前段時間做匿名委託的時候發現了一些挺好玩的事情

如果我去遍曆並執行一個List<Delegate>那麼我可以自由的控制是否要執行我需要的步驟

比如說List{A,B,C,D,E},A~E分別表示是否要去公園裡某個景點,

那麼我對這些事件進行窮舉,然後來了客人甲,他只要告訴我他想要去的幾個景點

我就可以給他安排路線(比如:執行順序CDABE)要是臨時取消或者想加個景點什麼的也很方便

於是,為了使得列表裡存放幾乎任何形式上的方法,我選擇使用Func<Object[], object>

但這樣會產生一個問題,那就是當我加入到List中的Delegate假設並不是Func<Object[], object>的時候怎麼辦?

所以我還是選擇使用Delegate來表示一切形式的委託,於是就套了一個Func<>殼

這樣一來我可以把任意形式上Delegate丟進Func<>裡,既有傳回值也能接受值

所以就出現了這個結果

 //這是一個列表

  public static List<Func<object[], Delegate>> delegateList = new List<Func<object[], Delegate>>

  {

  //這是列表裡的第一個元素,並且這是一個匿名委託,形式上是Func<object[], Delegate>

    //useless,形式上是Func<object[], Delegate>裡的object[],不過沒什麼用,我還沒找到必須要加這個的理由

    (useless) =>
    {

      //這是一個匿名委託,形式上是Delegate,這裡的例子是Func<object[],object>,並不僅限於這個

      Func<object[],object> function = (objs) =>
      {

        //這裡是傳回值,形式上是Func<object[],object>裡的object
        return new object();
      };

      //這裡是傳回值,形式上是Func<in T, out TResult>中的out TResult這部分
      return function;
    },

//這是列表裡的第二個元素

//這是一個匿名的委託

    (useless)=>{ Func<object[],object> function = (objs) =>{return new object();};},

//這是列表裡的第三個元素

//這是一個Acion的委託

    (useless)=>{ Action<object[]> function = (objs) =>{};},
  };

public class MoeEvent    {
//新增一個方法,這個是插入,List裡會多一個元素
public void AddEvent(Func<object[], object> function) { if (function.GetType() == typeof(Func<object[], object>)) { Func<object[], Delegate> f = (useless) => { return function; }; MoeScript.delegateList.Add(f); } }
//把function方法合并到某事件的前面,這個是多路廣播,List裡不會多出一個元素
public void AddPrefixToEvent(int index, Func<object[], object> function) { if (index < MoeScript.delegateList.Count && index >= 0) { if (function.GetType() == typeof(Func<object[], object>) && MoeScript.delegateList[index].GetType() == typeof(Func<object[], Delegate>)) { Func<object[], object> oldfc = (Func<object[], object>)MoeScript.delegateList[index](new object[] { }); Func<object[], Delegate> fc = (useless) => { Func<object[], object> newfc = function + oldfc;//區別在這裡 return newfc; }; MoeScript.delegateList[index] = fc; } } }
//把function方法合并到某事件的後面,這個是多路廣播,List裡不會多出一個元素
public void AddSuffixToEvent(int index, Func<object[], object> function) { if (index < MoeScript.delegateList.Count && index >= 0) { if (function.GetType() == typeof(Func<object[], object>) && MoeScript.delegateList[index].GetType() == typeof(Func<object[], Delegate>)) { Func<object[], object> oldfc = (Func<object[], object>)MoeScript.delegateList[index](new object[] { }); Func<object[], Delegate> fc = (useless) => { Func<object[], object> newfc = oldfc + function;//區別在這裡 return newfc; }; MoeScript.delegateList[index] = fc; } } } }

 

匿名事件類別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.