Improve Code Reuse (Python-style decorator for c#?)
Improve Code Reuse (Python-style decorator for c#?) I'm moving from python to c#, and one of the things I miss is python-style decorators. If I had repeated code at the top of a load of functions (validation checking etc), I could create a decorator to do it. I've seen that there are c# decorators of a sort, but they look like they work more on the class level. Although I am a bit confused by them. Regardless - how would you go about improving code re-use/DRY in this function? All the stuff in the function is common, except the two places marked. Its callback driven Tcp requests to a server, with a block to stop multiple concurrent requests (check for Idle state). public void MyFunction(string apples, Action<TcpRequest> onSuccess=null, Action<TcpRequest> onError=null) { // Throw exception if already busy with an operation if (!state.Has(State.Idle)) { throw new OperationInProgress(); } // Define callback action Action<TcpRe...
