How do I create a new delegate type based on an existing one, in C#?

前端 未结 4 1227
别那么骄傲
别那么骄傲 2021-02-12 21:23

Is there any way that I can create a new delegate type based on an existing one? In my case, I\'d like to create a delegate MyMouseEventDelegate which would have th

4条回答
  •  孤街浪徒
    2021-02-12 22:04

    I didn't believe jon-skeet. I had to try it for myself. He was right.

    public void F(object sender, MouseEventArgs e) {}; 
    public MyLeftClickHandler l;
    public MyRightClickHandler r;
    public void Test()
    {
        l = F;  // Automatically converted.
        r = F;  // Automatically converted to a different type.
        l = r;  // Syntax error.  Incompatible types.
        l = new MyLeftClickHandler(r);  // Explicit conversion works.
    }
    

    That wasn't obvious!

提交回复
热议问题