Question

I have a FTPDataTransfer-class that has a state (FTPDataTransferState). This class provides a lot of transfer-methods like ReceiveData (overloaded), SendData, ... How can I change the state to Ready, Transfer, ... without chaning the value in every transfer-method?

Was it helpful?

Solution

You can use PostSharp for this. In more detail, the OnMethodBoundaryAspect is the aspect you want to use. In your case it could look like:

using PostSharp.Aspects;

[Serializable]
public sealed class ReadyOnExit : OnMethodBoundaryAspect
{
    public override void OnExit(MethodExecutionArgs args)
    {
        var state = (FTPDataTransferState)args.Instance;
        state.Transfer(FTPDataTransferState.Ready);
    }
}

OTHER TIPS

The leading AOP toolkit for .NET is PostSharp. The way it would work in your case is that you'd define a custom attribute specifying the state that should be set when the method is to execute, apply the attribute to the appropriate methods, and define (in one place) the code setting/resetting the state. The toolkit would make this code run when the methods are entered/left.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top