Firing an event / function on a property? (C#)

后端 未结 8 1470
你的背包
你的背包 2021-02-08 11:46

I am using a class that I cannot edit, it has a property (a boolean) of which it would be nice to be informed when it changes, I can\'t edit the properties get or set as I am im

8条回答
  •  不知归路
    2021-02-08 12:44

    You cant do this directly [as Jon Skeet said], unless it's virtual, you're in a position to intercept all instance creations of the class and there are no changes to a backing field that influences the real 'value' of the propget.

    The only way to brute force this is to use Mono.Cecil or MS CCI to instrument the prop setter a la this DimeCast on Cecil. (Or PostSharp)

    However this wouldn't trap internal changes to the backing field (if there even is one). (Which is why wrapping probably wont work).

    UPDATE: Given your update that you're definitely trying to trap the underlying field change, the only way to do that is to use PS / CCI / Cecil and analyse the flow to intercept all field updates. In short, not very feasible.

提交回复
热议问题