Avoid duplicate event subscriptions in C#

前端 未结 5 1982
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-25 15:28

How would you suggest the best way of avoiding duplicate event subscriptions? if this line of code executes in two places, the event will get ran twice. I\'m trying to avoid

5条回答
  •  天命终不由人
    2020-12-25 15:56

    If you own the source for the class of theObject, then you have access to the InvocationList of TheEvent. You can implement your own add accessor for the event and check before adding.

    However, I think that your approach is fine too.

提交回复
热议问题