用《斗破苍穹》的视角打开C#委托2 委托链 / 泛型委托 / GetInvocationList

委托链

  • 经过不懈地努力,我终于成为了斗师,并成功掌握了两种斗技------八极崩和焰分噬浪尺。
  • 于是,我琢磨着,能不能搞一套连招,直接把对方带走。
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FrameworkDemo
{
    class Program
    {
        delegate void 攻击委托类型(string a);

        static void 八极崩(string a) {
            Console.WriteLine("八极崩");
        }
        static void 焰分噬浪尺(string a)
        {
            Console.WriteLine("焰分噬浪尺");
        }
        static void Main(string[] args)
        {
            // 创建委托链
            攻击委托类型 一套连招 = new 攻击委托类型(八极崩);
            // 攻击委托类型 一套连招 = 八极崩;   // 也可以这样写
            一套连招 += 焰分噬浪尺;
            // 启动委托
            一套连招("吃我一记连招");
            Console.ReadLine();
        }
    }
}

委托链返回值(GetInvocationList)

  • 这连招确实是打出来了,但是我怎么知道我打出了多少伤害呢?所以我要想办法接受这套输出的反馈。
csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FrameworkDemo
{
    class Program
    {
        delegate int 攻击委托类型(string a);

        static int 八极崩(string a) {
            Console.WriteLine("八极崩");
            return 330;
        }
        static int 焰分噬浪尺(string a)
        {
            Console.WriteLine("焰分噬浪尺");
            return 660;
        }
        static void Main(string[] args)
        {
            // 创建委托
            攻击委托类型 一套连招 = 八极崩;
            一套连招 += 焰分噬浪尺;
            // 获取委托链
            Delegate[] delList = 一套连招.GetInvocationList();
            // 遍历委托链
            for (int i = 0; i < delList.Length; i++) {
                攻击委托类型 del = (攻击委托类型)delList[i];
                // 一次调用委托并获取返回值
                int result = del("吃我一套连招");
                Console.WriteLine($"攻击伤害:{result}");
            }
            Console.ReadLine();
        }
    }
}

泛型委托

  • 这时候,问题就来了,我每创建一个委托,岂不是都要先定义一个委托类型,然后再创建一个委托实例,假设这个委托类型只用一次,那我岂不是要在我的类里面定义一大堆的委托?
  • 于是乎,就出现了一系列的泛型委托。
  • Action是不带有返回值的泛型,而Func具有返回值。
csharp 复制代码
using System;

namespace FrameworkDemo
{
    class Program
    {
        static void 八极崩() {
            Console.WriteLine("八极崩");
        }
        static void 焰分噬浪尺(string a)
        {
            Console.WriteLine("焰分噬浪尺");
        }
        static string 佛怒火莲(string a) {
            Console.WriteLine("佛怒火莲");
            return "成功击杀敌军!";
        }

        static void Main(string[] args)
        {
            // 不带返回值的委托
            Action 攻击委托1 = 八极崩;
            攻击委托1();
            Action<string> 攻击委托2 = 焰分噬浪尺;
            攻击委托2("吃我一击!");
            // 带返回值的委托
            // <>中的最后一个参数是函数的返回值类型
            Func<string, string> 攻击委托3 = 佛怒火莲;
            string result = 攻击委托3("绝杀");
            Console.ReadLine();
        }
    }
}
相关推荐
m0_74824534几秒前
python——Django 框架
开发语言·python·django
曼巴UE55 分钟前
UE5.3 C++ TArray系列(一)
开发语言·c++·ue5
熬夜苦读学习16 分钟前
Linux文件系统
linux·运维·服务器·开发语言·后端
菜鸟一枚在这24 分钟前
深度解析建造者模式:复杂对象构建的优雅之道
java·开发语言·算法
阿巴~阿巴~43 分钟前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
奔跑吧邓邓子2 小时前
【Python爬虫(34)】Python多进程编程:开启高效并行世界的钥匙
开发语言·爬虫·python·多进程
pchmi2 小时前
CNN常用卷积核
深度学习·神经网络·机器学习·cnn·c#
Heris992 小时前
2.22 c++练习【operator运算符重载、封装消息队列、封装信号灯集】
开发语言·c++
----云烟----2 小时前
C/C++ 中 volatile 关键字详解
c语言·开发语言·c++
yuanpan3 小时前
23种设计模式之《组合模式(Composite)》在c#中的应用及理解
开发语言·设计模式·c#·组合模式