vendredi 19 août 2022

Is adding pre/post invocation logic to original method possible for C# properties using standard reflection libs?

Currently I'm working with a project on Unity3d engine and want to implement "Observer component" which will listen for property changing in other components.

The good idea is implement https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-implement-property-change-notification?view=netframeworkdesktop-4.8, but, I don't want to implement it in each component I want to "listen".

So, my original vision (if simplify it) is

  1. Get the "MonoBehaviour" via GameObject reference
  2. Get all properties via Reflection (GetType().GetProperties())
  3. Modify "set" method by adding to it "preInvocation/postInvocation" logic.

The second way is get the reference to the property, and check the value each "Update" method. But, this way (maybe?) is more slowly.

Also, I found the good library for adding listeners before/after methods https://harmony.pardeike.net/articles/intro.html, but, not sure, if I really need to use this library for only this case

Maybe, there is more easy way using standard c# functionality?

P.S. Sorry for my bad english





Aucun commentaire:

Enregistrer un commentaire