在值类型定义的扩展方法不能用于创建代表 - 为什么不呢?定义、类型、代表、方法

2023-09-02 21:50:25 作者:那与爱无关″

扩展方法可以分配给匹配的对象在他们的使用,这样的代表:

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

这给了

错误CS1113:扩展方法FunnyExtension.Double(INT)定义   价值型'诠释'不能用于创建代表。

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

他们为什么不能?

推荐答案

在回应我的其他回答,埃里克·史密斯正确地指出:

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

......因为它需要隐式拳接收器类型参数...。这是反正会发生什么,如果你做这样的事情:函数F = 5.ToString;这是完全合法的。

"... 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:

在结构普通实例的方法服用,在CIL层面,一个管理指针(类型&安培; )作为接收机参数。这是必要的,这样的结构实例方法可以分配给结构的领域。请参阅Partition二,第13.3节。

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.

同样,在类的实例方法采取对象引用(类型 0 )作为接收参数(不同的是,这是一个指向托管堆,和需要进行跟踪用于GC)。

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).

由于两个CIL &安培; 0 S能(并)通过指针实现的,一切都没说的委托执行。无论委托是否捕捉静态方法,一个类的实例方法,还是结构实例方法,所有需要做的是将指针传递给它的 _target 第一个参数功能

Since both CIL &s and Os 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.

但情况下,我们正在讨论的废墟。服用 INT 作为第一个参数需要类型的CIL参数 INT32 (请参阅分区三,部分静态扩展方法1.1.1)。 这里是事情出轨。我看不出有任何理由为什么它不会的可能的为代表的实施要认识到这是发生(的例如,通过检查的MethodInfo的被捕获相关的元数据),并发出一个thunk,将拆箱 _target 并传递作为第一个参数,而这个ISN牛逼需要代表经典实例方法的结构,因为他们期待一个指针反正的并没有出现(在例子中我先前不正确的答案判断)来实现。显然,在讨论的特定值类型将控制所需thunk的确切性质。

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.

除非我缺少一个更为根本的障碍,实现(我能想象,这将造成的验证问题,例如),这似乎是一个合理的情况下可以延长运行时支持这种情况下进行,但所有种种迹象都指向了运行时的这是一个限制,不是C#编译器本身。

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.