Implement method decorators in C#

前端 未结 4 1528
忘了有多久
忘了有多久 2020-12-31 12:48

In python is possible to implement function decorators to extend the behavior of functions and methods.

In particular I\'m migrating a devi

4条回答
  •  清酒与你
    2020-12-31 13:29

    There's no easy way to implement such decorators in C# - custom Attributes are by default only descriptive. There are however projects that extend C# compiler or runtime so that you can actually use this. I think the best one is PostSharp. With it you can define such method decorator ("aspect" in general) and the method gets wrapped during compilation like you need.

    I've also seen this implemented by actually wrapping your classes by decorator classes, but that's a lot of work and I don't think it can be done in a really general way. Wikipedia shows this in Decorator Pattern article

提交回复
热议问题