欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#委托方法Func()中GetInvocationList()方法的用法介紹

 更新時間:2022年01月14日 08:47:19   作者:癡者工良  
這篇文章介紹了C#委托方法Func()中GetInvocationList()方法的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在日常使用委托時,有以下常用方法

方法名稱說明
 Clone  創(chuàng)建委托的淺表副本。
 GetInvocationList  按照調(diào)用順序返回此多路廣播委托的調(diào)用列表。
 GetMethodImpl  返回由當(dāng)前的 MulticastDelegate 表示的靜態(tài)方法。
 GetObjectData  用序列化該實例所需的所有數(shù)據(jù)填充 SerializationInfo 對象。
 MemberwiseClone  創(chuàng)建當(dāng)前 Object 的淺表副本。
 RemoveImpl  調(diào)用列表中移除與指定委托相等的元素

GetInvocationList() 的用途

當(dāng)委托有多個返回值時

當(dāng)你編寫一個 delegate委托 或 Func<>泛型委托 ,并為實例綁定多個方法時,每個方法都有一個返回值??赡軙龅竭@種情況:

class Program
    {
        public static string a(string str)
        {
            Console.WriteLine("方法a");
            return str+"方法a";
        }
        public static string b(string str)
        {
            Console.WriteLine("方法b");
            return str + "方法b";
        }
        public static string c(string str)
        {
            Console.WriteLine("方法c");
            return str + "方法c";
        }
        static void Main(string[] args)
        {
            Func<string, string> func=a;
            func += b;
            func += c;
            Console.WriteLine(func("測試"));
            Console.ReadKey();
        }

    }

調(diào)用委托后,只能獲取到最后一個調(diào)用方法的返回值。

使用 GetInvocationList()

GetInvocationList() 能夠返回 這個委托的方法鏈表。

通過使用循環(huán),把每個方法順序調(diào)用一次,每次循環(huán)中都會產(chǎn)生當(dāng)前調(diào)用方法的返回值。

class Program
{
    public static string a(string str)
    {
        Console.WriteLine("方法a");
        return str+"方法a";
    }
    public static string b(string str)
    {
        Console.WriteLine("方法b");
        return str + "方法b";
    }
    public static string c(string str)
    {
        Console.WriteLine("方法c");
        return str + "方法c";
    }
    static void Main(string[] args)
    {
        Func<string, string> func=a;
        func += b;
        func += c;
        var funclist = func.GetInvocationList();
        foreach (Func<string, string> f in funclist)
        {
            Console.WriteLine(f("測試"));
        }
        Console.ReadKey();
    }

相當(dāng)于把委托里順序調(diào)用的方法分離成一個列表,通過循環(huán)調(diào)用,循環(huán)獲取。

以上所述是小編給大家介紹的C#委托方法Func()中GetInvocationList()方法的用法,希望對大家有所幫助。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論