MSIL - how do you invoke a private method from MSIL?

后端 未结 3 1165
礼貌的吻别
礼貌的吻别 2021-01-02 20:04

I\'m writing a \"weak event factory\" - code which converts any Delegate into a new delegate with an identical signature, but with implementing a WeakReference on the target

3条回答
  •  粉色の甜心
    2021-01-02 20:47

    Use Call, not Callvirt.

    [Edit: Not as a general recommendation, but specifically to address this issue]

    Callvirt is for calling virtual methods, where the destination address also depends upon the exact type of the instance. That doesn't work when you're using a weak reference.

    The target of a private method, on the other hand, can be determined at compile-time. It is therefore appropriate to invoke it using Call.

提交回复
热议问题