委托的合并
public delegate void TestDeleteType(); 
class Program
{
    private static TestDeleteType dd = null;
    static void Main(string[] args)
    {
        dd = Delegate.Combine(dd, new TestDeleteType( HaHa1)) as TestDeleteType;
        dd();//HaHa1

        Action a = null;
        a = Delegate.Combine(a, new Action(HaHa1)) as Action;
        a();//HaHa1
        //a = Delegate.Combine(a, dd) as Action; 异常,必须要有相同的类型
        //a();

        Console.ReadKey();
    }

    static void HaHa1()
    {
        Console.WriteLine("HaHa1");
    }
}


可以使用Delegate.Combine()整合多个同一样类型的委托,可以看出作为参数的委托是可以为空的,返回一个Delegate类型,需要自己转换一下

也可以使用+直接联合委托,注意委托类型一定需要一致,如果有返回值,就只返回最后注册的函数的返回值。


首页 我的博客
粤ICP备17103704号