How to convert an action to a defined delegate of

2020-02-12 07:15发布

class Test
{
    public delegate void FruitDelegate(Fruit f);

    public void Notify<T>(Action<T> del) where T : Fruit
    {
        FruitDelegate f = del; // Cannot implicitly convert type 'Action<T>' to 'FruitDelegate
    }
}

Fruit is an empty class. Both of these delegates have the same signature.

I cannot seem to get any of this working. Maybe it would help if I explained what I am trying to do (provide some context).

I want to create a class that has a generic static method that provides a type and a method callback (like the above example).

The problem I am having is that the delegate contains a parameter and I don't want to have to cast it within the method callback. For example, I want this:

public void SomeMethod()
{
    Test.Notify<Apple>(AppleHandler);
}

private void AppleHandler(Apple apple)
{

}

Instead of this:

public void SomeMethod()
{
    Test.Notify<Apple>(AppleHandler);
}

private void AppleHandler(Fruit fruit)
{
    Apple apple = (Apple)fruit;
}

Is this kind of thing possible? Have been working on it for a few hours without much luck =/

3条回答
贪生不怕死
2楼-- · 2020-02-12 07:23

is this what you want?

static void Main(string[] args)
{

    Program p = new Program();
    p.SomeMethod();
}

public class Fruit
{ }

public class Apple : Fruit { }

public delegate void FruitDelegate<in T>(T f) where T : Fruit;

class Test
{
    public static void Notify<T>(FruitDelegate<T> del)
        where T : Fruit, new()
    {
        T t = new T();
        del.DynamicInvoke(t);
    }
}

private void AppleHandler(Apple apple)
{
    Console.WriteLine(apple.GetType().FullName);
}

public void SomeMethod()
{
    FruitDelegate<Apple> del = new FruitDelegate<Apple>(AppleHandler);
    Test.Notify<Apple>(del);
}
查看更多
姐就是有狂的资本
3楼-- · 2020-02-12 07:27

There is good reason you cannot do this. Suppose the rest of your method was:

class Test
{
    public delegate void FruitDelegate(Fruit f);

    public void Notify<T>(Action<T> del) where T : Fruit
    {
        FruitDelegate f = del; 
        f(new Banana());  //should be legal, but del may be Action<Apple>
    }
}

That would definitely not work, so the compiler is correct here.

查看更多
Root(大扎)
4楼-- · 2020-02-12 07:41

What about something like this?

public void Notify<T>(Action<T> 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.

查看更多
登录 后发表回答