C#: How to create an attribute on a method triggering an event when it is invoked?

前端 未结 7 874
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 02:10

Is there a way in C# or .NET in general to create an attribute on a method which triggers an event when the method is invoked? Ideally, I would be able to run custom actions

7条回答
  •  一整个雨季
    2020-11-29 02:17

    I don't think there is a way to do it with just an attribute, but using proxy classes and reflection you could have a class that knows to intercept instantiations of the classes in which you have attributed methods.

    Then the proxy class can trigger an event whenever the attributed methods are called.

提交回复
热议问题