Adding Events Using Extension Methods

I found some messages about raising events using the extension method, however my question is a little different:

I have an interface like this:

public interface IStateMachine 
{
    void SetState(IState NewState);
    IState GetState();
}

Using this interface, I can create an extension method, for example:

public static void ChangeState(this IStateMachine StateMachine, IState NewState) 
{
    StateMachine.GetState().Exit();
    StateMachine.SetState(NewState);
    StateMachine.GetState().Enter();
}

I really want there to be events that need to be fired, for example: a statechange-event.

However, I do not want to StatechangeEventbe part of the interface IStateMachine, but it seems that this is the only way. I have several classes that implement IStateMachine, and so I have to repeat the same code every time.

+5
source share
2 answers

In C #, there are only extension methods. No properties or extension events.

.
, .

"" -. #.

Subscribe Unsubscribe IStateMachine.

StateMachineSubscribers, Subscribe Unsubscribe. , Raise, ChangeState.

+4

, IStateMachine. , SetState, - , , .

0

All Articles