Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Delegate use with Generic Method to return Task<T>

I’ m missing something here. I ‘ll be as clear as I can be.

let’s say I have this method :

    public Task<CustomClass> TestMethod(string one, string two, int three)
        {
            throw new NotImplementedException();
        }

and I want to create a generic method that takes methods like these (through a delegate) and does something.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

so let’s say that I have this class with a delegate and a generic method:

public static class SomeClass
{
    public delegate T CallbackMethodDelegate<T>(params object[] args);
    public static void InvokeMethod<T>(CallbackMethodDelegate<Task<T>> method, params object[] args) where T: class
        {
            method.Invoke(args);
        }

}

my understanding says that T can be a class, so I should be able to do this somewhere in the code :

    SomeClass.InvokeMethod<CustomClass>(TestMethod, "one", "two", 3);

But I get compile error

CS1503:Argument 1: cannot convert from 'method group' to 'SomeClass.CallBackMethodDelegate<Task<CustomClass>>'

I thought it might be an async thing but even if I change the delegate to void I get the same error. Any ideas what I’m missing? Seems like I’m misunderstanding something important about delegates or generics here. Thank you all in advance.

>Solution :

as the error suggests your method is not compatible with the delegate. The delegate requires object[] as a parameter your method has 3 explicit parameters string,string,int this is not compatible.

what you can do if you really need this is you can create a set of invoke methods that cover wide range of methods:

void InvokeMethod<TResult>(Func<Task<TResult>> method, TParam1 param1)
void InvokeMethod<TParam1, TResult>(Func<TParam1,Task<TResult>> method, TParam1 param1)
void InvokeMethod<TParam1,TParam2, TResult>(Func<TParam1, TParam2 ,Task<TResult>> method, TParam1 param1, TParam2 param2)

and so on as many parameters you need to have.

you can reduce those methods to single implementation that is not type safe but not accessible directly

void InvokeMethodImpl(Delegate method, params object[] args)
   => method.DynamicInvoke(args);

so all generic methods would just call this method:

 void InvokeMethod<TParam1, TResult>(Func<TParam1,Task<TResult>> method, TParam1 param1) 
      => InvokeMethodImpl(method, param1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading