/// <summary> /// Initializes the state. /// <param name="monitor">Monitor</param> /// <param name="isHot">Is hot</param> /// <param name="isCold">Is cold</param> internal void InitializeState(Monitor monitor, bool isHot, bool isCold) { this.Monitor = monitor; this.IsHot = isHot; this.IsCold = isCold; this.GotoTransitions = new GotoStateTransitions(); this.ActionBindings = new ActionBindings(); this.IgnoredEvents = new HashSet<Type>(); var entryAttribute = this.GetType().GetCustomAttribute(typeof(OnEntry), false) as OnEntry; var exitAttribute = this.GetType().GetCustomAttribute(typeof(OnExit), false) as OnExit; if (entryAttribute != null) { var method = this.Monitor.GetType().GetMethod(entryAttribute.Action, BindingFlags.NonPublic | BindingFlags.Instance); var action = (Action)Delegate.CreateDelegate(typeof(Action), this.Monitor, method); this.EntryAction = action; } if (exitAttribute != null) { var method = this.Monitor.GetType().GetMethod(exitAttribute.Action, BindingFlags.NonPublic | BindingFlags.Instance); var action = (Action)Delegate.CreateDelegate(typeof(Action), this.Monitor, method); this.ExitAction = action; } var gotoAttributes = this.GetType().GetCustomAttributes(typeof(OnEventGotoState), false) as OnEventGotoState[]; var doAttributes = this.GetType().GetCustomAttributes(typeof(OnEventDoAction), false) as OnEventDoAction[]; foreach (var attr in gotoAttributes) { if (attr.Action == null) { this.GotoTransitions.Add(attr.Event, attr.State); } else { var method = this.Monitor.GetType().GetMethod(attr.Action, BindingFlags.NonPublic | BindingFlags.Instance); var action = (Action)Delegate.CreateDelegate(typeof(Action), this.Monitor, method); this.GotoTransitions.Add(attr.Event, attr.State, action); } } foreach (var attr in doAttributes) { var method = this.Monitor.GetType().GetMethod(attr.Action, BindingFlags.NonPublic | BindingFlags.Instance); var action = (Action)Delegate.CreateDelegate(typeof(Action), this.Monitor, method); this.ActionBindings.Add(attr.Event, action); } var ignoreEventsAttribute = this.GetType().GetCustomAttribute(typeof(IgnoreEvents), false) as IgnoreEvents; if (ignoreEventsAttribute != null) { this.IgnoredEvents.UnionWith(ignoreEventsAttribute.Events); } }
/// <summary> /// Configures the state transitions of the monitor. /// </summary> /// <param name="state">State</param> private void ConfigureStateTransitions(MonitorState state) { this.GotoTransitions = state.GotoTransitions; this.ActionBindings = state.ActionBindings; this.IgnoredEvents = state.IgnoredEvents; }
/// <summary> /// Initializes the state. /// </summary> internal void InitializeState() { this.IsStart = false; this.GotoTransitions = new GotoStateTransitions(); this.PushTransitions = new PushStateTransitions(); this.ActionBindings = new ActionBindings(); this.IgnoredEvents = new HashSet<Type>(); this.DeferredEvents = new HashSet<Type>(); var entryAttribute = this.GetType().GetCustomAttribute(typeof(OnEntry), false) as OnEntry; var exitAttribute = this.GetType().GetCustomAttribute(typeof(OnExit), false) as OnExit; if (entryAttribute != null) { this.EntryAction = entryAttribute.Action; } if (exitAttribute != null) { this.ExitAction = exitAttribute.Action; } var gotoAttributes = this.GetType().GetCustomAttributes(typeof(OnEventGotoState), false) as OnEventGotoState[]; var pushAttributes = this.GetType().GetCustomAttributes(typeof(OnEventPushState), false) as OnEventPushState[]; var doAttributes = this.GetType().GetCustomAttributes(typeof(OnEventDoAction), false) as OnEventDoAction[]; foreach (var attr in gotoAttributes) { if (attr.Action == null) { this.GotoTransitions.Add(attr.Event, attr.State); } else { this.GotoTransitions.Add(attr.Event, attr.State, attr.Action); } } foreach (var attr in pushAttributes) { this.PushTransitions.Add(attr.Event, attr.State); } foreach (var attr in doAttributes) { this.ActionBindings.Add(attr.Event, attr.Action); } var ignoreEventsAttribute = this.GetType().GetCustomAttribute(typeof(IgnoreEvents), false) as IgnoreEvents; var deferEventsAttribute = this.GetType().GetCustomAttribute(typeof(DeferEvents), false) as DeferEvents; if (ignoreEventsAttribute != null) { this.IgnoredEvents.UnionWith(ignoreEventsAttribute.Events); } if (deferEventsAttribute != null) { this.DeferredEvents.UnionWith(deferEventsAttribute.Events); } if (this.GetType().IsDefined(typeof(Start), false)) { this.IsStart = true; } }