Extension methods defined on value types cannot be used to create delegates

Extension methods can be assigned to delegates that match their usage on an object, like this:

static class FunnyExtension {
    public static string Double(this string str) { return str + str; }
    public static int Double(this int num) { return num + num; }
}


Func<string> aaMaker = "a".Double;
Func<string, string> doubler = FunnyExtension.Double;

Console.WriteLine(aaMaker());       //Prints "aa"
Console.WriteLine(doubler("b"));    //Prints "bb"

If the type they're extending is a value type, it won't work:

Func<int> eightMaker = 4.Double;    //Error CS1113: Extension methods 'FunnyExtension.Double(int)' defined on value type 'int' cannot be used to create delegates
Func<int, int> intDoubler = FunnyExtension.Double;  //Works

This gives

Error CS1113: Extension methods 'FunnyExtension.Double(int)' defined on value type 'int' cannot be used to create delegates.

Why can't they?


In response to my other answer, Eric Smith correctly notes:

"... because it would require implicitly boxing the receiver type parameter ...". Which is what happens anyway, if you do something like this: Func f = 5.ToString; Which is perfectly legal.

Thinking about this has led me to a new answer. Try this on for size:

Ordinary "instance" methods on structs take, at the CIL level, a "managed pointer" (type & ) as a receiver parameter. This is necessary so that instance methods on structs can assign to fields of the struct. See Partition II, Section 13.3.

Similarly, instance methods on classes take an "object reference" (type O ) as a receiver parameter (the difference being that this is a pointer to the managed heap, and needs to be tracked for GC).

Since both CIL & s and O s can be (and are) implemented by pointers, everything is hunky-dory for the delegate implementation. Regardless of whether a delegate captures a static method, a class instance method, or a struct instance method, all it needs to do is pass the pointer to its _target to the first argument of the function.

But the scenario we are discussing ruins that. A static extension method taking an int as a first argument requires a CIL argument of type int32 (see Partition III, section 1.1.1). Here is where things go off the rails. I don't see any reason why it wouldn't be possible for the implementation of delegates to realize that this was happening (for example, by inspecting the metadata associated with the MethodInfo being captured) and emit a thunk that would unbox the _target and pass that as the first argument, but this isn't needed for delegates to classical instance methods on structs, since they expect a pointer anyway and doesn't appear (judging by the example in my earlier incorrect answer) to be implemented. Obviously the specific value type in question would control the exact nature of the required thunk.

Unless I am missing a more fundamental obstacle to implementation (I could imagine that it would pose problems for the verifier, for example), it seems like a reasonable case could be made for extending the runtime to support this case, but all the signs are pointing towards this being a limitation of the runtime and not of the C# compiler per se.


EDIT 2 I don't believe this answer anymore, but I left it here so the thread would still make sense and so that people would see why it isn't right. See my other answer for a different take on the matter.

Original

Because it would require implicitly boxing the value type receiver parameter (because the _target field in the System.Delegate type which holds the the receiver parameter is of type System.Object), which could lead to some strange aliasing behavior if you weren't expecting it.

EDIT

There is something else going on here. I ran this sample program:

class Program
{
    public static int Combine(int a, int b)
    {
        return a + b;
    }

    static void Main(string[] args)
    {
        var combineMethod = typeof(Program).GetMethod("Combine");
        var add4 = Delegate.CreateDelegate(
                              typeof(Converter<int, int>),
                              4,
                              combineMethod) as Converter<int, int>;

        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine(add4(i));
        }
        Console.ReadLine();
    }
}

and got an ArgumentException: "Error binding to target method." at the call to CreateDelegate. I'm not sure why, and because the relevant method is an internalcall method, Reflector isn't much help. The documentation for CreateDelegate also wasn't much help. I'm sure it has something to do with boxing the receiver, maybe someone with knowledge of the Rotor source could help explain why?

链接地址: http://www.djcxy.com/p/52876.html

上一篇: C函数计算参数的总大小

下一篇: 在值类型上定义的扩展方法不能用于创建委托