0

In the below code snippet, the call to CreateDelegate with the 64-bit enum throws an ArgumentException "Error binding to target method". Yet the 32-bit enum works fine.

Could someone tell me why?

class Test
{
    public static void DoIt()
    {
        Func<int, int> f32 = i => i;
        var d32 = (Func<int, E32>)Delegate.CreateDelegate(typeof(Func<int, E32>), f32.Method);

        Func<long, long> f64 = i => i;
        var d64 = (Func<long, E64>)Delegate.CreateDelegate(typeof(Func<long, E64>), f64.Method);
    }
}

enum E32 { };
enum E64 { };

(For some context on this weird code, it's inspired by this answer: https://stackoverflow.com/a/4026609/14582)

Is there something special about 64-bit enums that makes this fail, perhaps an unimplemented code path in binding argument matching code? Or is it a quirk that 32-bit enum works in this way?

Community
  • 1
  • 1
scobi
  • 14,252
  • 13
  • 80
  • 114

2 Answers2

2

The base type of E64 is int not long which makes it incompatible with a function that has a return type of long. The f32 version works because E32 and int are compatible types.

You can fix this sample by having E64 set it's base type to long

enum E64 : long {}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Yeah I realized this 5 seconds after posting. Oops. – scobi Jan 09 '12 at 23:49
  • 1
    @ScottBilas sometimes the easiest way to figure out the answer is to explain the problem to someone else :). – JaredPar Jan 09 '12 at 23:49
  • Totally. :) The bar is very high here too. Really need to make sure my premises are correct before hitting the almighty submit button. 99% is not good enough! – scobi Jan 09 '12 at 23:59
0

That's not a 64-bit enum.
You need to add : long.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964