Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 614
Ryan Peschel Avatar asked Oct 20 '11 02:10

Ryan Peschel


1 Answers

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);
}
like image 139
ojlovecd Avatar answered Nov 06 '22 08:11

ojlovecd