Search code examples
c#unity-game-enginegree

Is there something like python decorators for c#?


I am wrapping calls to an API and most of my methods have in their first lines:

if ( !Gree.Authorizer.IsAuthorized() )
{
    return;
}

In python I would decorate those methods with something like @login_required.

What would you use to refactor that type of logic in c#?


Solution

  • You are looking for a subset of a more general programming methodology called Aspect Oriented Programming.

    C# seems to support it through several libraries, and one can also roll out his own, thanks to some of the CLR features. See Aspect Oriented Programming using .NET which covers its basic principles (I am linking the part of the article talking about the specifics of C#, but the rest is equally interesting if you are looking for ready-made solutions like PostSharp, as mentioned in another answer).