I think I understood this problem; you are having problems calling the common delegate from the immediate window when the delegate compile time type is an open generic type. Here's a simpler reprogramming:
static void Main() { Test<double>(); } static void Test<T>() { Action<T> genericDel = delegate { };
Now, if I try to execute this delegate from the Test
method (by placing a breakpoint and using the immediate window), like this:
genericDel(42D);
I get the following error:
Delegate 'System.Action<T>' has some invalid arguments
Please note that this is not an exception, as you stated, but rather a “immediate window version” compile time error CS1594 .
Note that such a call would not be the same at compile time, because there is no explicit or explicit conversion from double
to T
This is inconsistent with the drawback of the direct window (it does not seem to want to use additional “knowledge at runtime” to help you in this case), but it can be argued that this is reasonable behavior, since the equivalent call made at compile time (in the original code) would also be illegal. However, it looks like a corner option; the direct window is perfectly capable of assigning shared variables and executing other code that would be illegal at compile time. Perhaps Roslyn will make things a lot more consistent.
If you want, you can work around this like this:
genericDel.DynamicInvoke(42D);
(or)
((Action<double>)(object)genericDel)(42D);
source share