How can I create an open Delegate from a struct's instance method?

后端 未结 3 1684
鱼传尺愫
鱼传尺愫 2020-12-03 23:44

I have a struct with a private method that I\'d like to invoke. Since I plan to do this in a performance critical section, I\'d like to cache a delegate to perform the actio

3条回答
  •  情深已故
    2020-12-03 23:55

    The first parameter of an unbound instance method delegate cannot be a value type. This is because of how value types must be handled when used as 'this' parameters. You can't simply pass them by value (as would occur if you were passing a value type as the first parameter of a static method) as then the method is acting on a copy and any mutation of the copy has no effect on the original.


    UPDATE: As noted in another answer, value types when used as 'this' parameters are effectively passed by reference.

提交回复
热议问题