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

前端 未结 4 1525
温柔的废话
温柔的废话 2021-02-12 21:46

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:00

    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!

提交回复
热议问题