0

I'm getting the following error: "cannot use a method group as an argument to a dynamically dispatched operation" on:

public static void Convert(dynamic o)
{
clsQRcode.ConvertToQRs(o, SendSignalR); // error is here
}

public static void SendSignalR(dynamic o)
{
.... do stuff ....
}

In clsQRcode.ConvertToQRs:

public static void ConvertToQRs(dynamic o, Action<dynamic> SSR)
{
... do stuff to o
SSR(o);
}

So, what am I don't wrong?

CHANGED CODE:

I removed all references to dynamics and now have the following code with a similar error:

public static void ConvertToQRs(string jsonString)
{
clsQRcode.ConvertToQRs(jsonString, SendSignalR); // error still here
}

public static string SendSignalR(string org_int, string person_int, string code, string message, string sCode = "")
{
... do stuff ...
}

Changed clsQRcode to:

public static void ConvertToQRs(string jsonString, Func<string, string, string, string, string> SSR)
{
... do the work ...
SSR(org_int, person_int, function, message);
}

But now the error message is: "cannot convert from 'method group' to 'Func

5
  • 2
    Possible duplicate of How to do Callbacks with Dynamic? Commented Mar 29, 2019 at 17:26
  • Actually, don't see anything in that sample that's passing a method with a dynamic parameter. Commented Mar 29, 2019 at 18:49
  • The top answer answers your question though, no? You're also unable to do a method group conversion in a dynamic call, and it's a dynamic call because o is dynamic. Commented Mar 29, 2019 at 18:51
  • I took all the dynamics out and just use a string. Still have a similar error. Commented Mar 29, 2019 at 20:00
  • Right, so different code and completely different error? Looks like the duplicate answered your first question, and the answer here answers your second. Commented Mar 29, 2019 at 21:41

2 Answers 2

2

Look at this line in your final code:

public static string SendSignalR(string org_int, string person_int, string code, string message, string sCode = "")

This method accepts 5 string parameters and returns string too, so it's Func<string, string, string, string, string, string> (first 5 string - types of input parameters, last one - type of return value), while here

public static void ConvertToQRs(string jsonString, Func<string, string, string, string, string> SSR)

SSR is Func<string, string, string, string, string> (note, only 5 string, not 6). Replace this line with

public static void ConvertToQRs(string jsonString, Func<string, string, string, string, string, string> SSR)

(6 string) and your code will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Really appreciate the specifics of your response. It worked.
0

This is a bit old now, but it's still an issue, so here I was. While the solution I devised is super jank, it works, is easy, and has minimal cost. Basically, structures aren't dynamic. Loophole! Better yet, Tuples are a thing now for super convenient structures.

Original code of consequence:

clsQRcode.ConvertToQRs(o, SendSignalR); // error is here

public static void ConvertToQRs(dynamic o, Action<dynamic> SSR)
{
    ... do stuff to o
    SSR(o);
}

Approach 1: Wrap them both into a Tuple (sadly, Tuples require a minimum of 2 elements)

clsQRcode.ConvertToQRs((o, SendSignalR)); // error WAS here

public static void ConvertToQRs((dynamic o, Action<dynamic> SSR) jankParm)
{
    ... do stuff to jankParm.o
    jankParm.SSR(jankParm.o);
}

Approach 2: Only put the dynamic(s) into the Tuple, creating an unused element if necessary. This allows you to also do parmy things with the callback and any other parameters, like making it optional.

clsQRcode.ConvertToQRs((o, null), SendSignalR); // error WAS here
clsQRcode.ConvertToQRs((o, null)); // leveraging the optional callback feature

public static void ConvertToQRs((dynamic o, dynamic unused) jankParm, Action<dynamic> SSR = null)
{
    ... do stuff to jankParm.o
    if (SSR != null) SSR(jankParm.o);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.