委托-利用GetInvocationList處理鏈式委托


在利用委托進行函數代理的時候,我們習慣於用+=來把一個符合條件的委托加入委托鏈之中,如果加入了多個這樣的函數,怎么一一對這些函數取返回值呢?請看下面的一個實例:

View Code
using System;

namespace GetInvocationListDaemonCAPP
{
    public delegate string TestDelegate();

    public class Program
    {
        static void Main(string[] args)
        {
            DelegateClass delegateClass = new DelegateClass();

            TestMethodOne one = new TestMethodOne();
            TestMethodTwo two = new TestMethodTwo();
            TestMethodThree three = new TestMethodThree();
            TestMethodFour four = new TestMethodFour();

            delegateClass.testDelegate += one.Say;
            delegateClass.testDelegate += two.Say;
            delegateClass.testDelegate += three.Say;
            delegateClass.testDelegate += four.Say;

            delegateClass.InvokeDelegate();

            Console.ReadKey();
        }
    }

    public class DelegateClass
    {
        public TestDelegate testDelegate;

        public void InvokeDelegate()
        {
            if (null != testDelegate)
            {
                string resultStr = testDelegate();
                Console.WriteLine(resultStr);
            }
        }
    }

    public class TestMethodOne
    {
        public string Say()
        {
            return "You called me from TestMethodOne~~~";
        }
    }

    public class TestMethodTwo
    {
        public string Say()
        {
            return "You called me from TestMethodTwo~~~";
        }
    }

    public class TestMethodThree
    {
        public string Say()
        {
            return "You called me from TestMethodThree~~~";
        }
    }

    public class TestMethodFour
    {
        public string Say()
        {
            return "You called me from TestMethodFour~~~";
        }
    }
}

在這個示例中,我用了一個委托代理了四個類型相同,返回值相同的函數,那么當我要獲取這些函數的返回值的時候,會得到什么樣的結果呢?

You called me from TestMethodFour~~~

結果就是上面的輸出,原來,像這種方式的委托操作,會保留最后一個輸出,前面幾個都被OverWrite掉了。
為了解決這個問題,GetInvocationList方法出現了。

View Code
using System;

namespace GetInvocationListDaemonCAPP
{
    public delegate string TestDelegate();

    public class Program
    {
        static void Main(string[] args)
        {
            DelegateClass delegateClass = new DelegateClass();

            TestMethodOne one = new TestMethodOne();
            TestMethodTwo two = new TestMethodTwo();
            TestMethodThree three = new TestMethodThree();
            TestMethodFour four = new TestMethodFour();

            delegateClass.testDelegate += one.Say;
            delegateClass.testDelegate += two.Say;
            delegateClass.testDelegate += three.Say;
            delegateClass.testDelegate += four.Say;

            delegateClass.InvokeDelegate();

            Console.ReadKey();
        }
    }

    public class DelegateClass
    {
        public TestDelegate testDelegate;

        public void InvokeDelegate()
        {
            if (null != testDelegate)
            {
                //遍歷委托鏈表
                foreach (Delegate dele in testDelegate.GetInvocationList())
                {
                    //類型轉換
                    TestDelegate delegateClass = (TestDelegate)dele;
                    
                    //調用並 得到返回結果
                    string resultStr = delegateClass();
                    Console.WriteLine(resultStr);
                }
            }
        }
    }

    public class TestMethodOne
    {
        public string Say()
        {
            return "You called me from TestMethodOne~~~";
        }
    }

    public class TestMethodTwo
    {
        public string Say()
        {
            return "You called me from TestMethodTwo~~~";
        }
    }

    public class TestMethodThree
    {
        public string Say()
        {
            return "You called me from TestMethodThree~~~";
        }
    }

    public class TestMethodFour
    {
        public string Say()
        {
            return "You called me from TestMethodFour~~~";
        }
    }
}

這樣操作后,得到的運行結果如下:

You called me from TestMethodOne~~~
You called me from TestMethodTwo~~~
You called me from TestMethodThree~~~
You called me from TestMethodFour~~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM