How to convert an action to a defined delegate of the same signature?

后端 未结 3 927
北海茫月
北海茫月 2020-12-19 09:30
class Test
{
    public delegate void FruitDelegate(Fruit f);

    public void Notify(Action del) where T : Fruit
    {
        FruitDelegate f = d         


        
3条回答
  •  情深已故
    2020-12-19 10:12

    What about something like this?

    public void Notify(Action del) where T : Fruit
    {
        FruitDelegate f = fruit => del((T)fruit);
    }
    

    The FruitDelegate instance, when invoked, would throw an InvalidCastException if, say, an AppleHandler was invoked with a Banana argument.

提交回复
热议问题