예제 #1
0
        /// <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);
            }
        }
예제 #2
0
 /// <summary>
 /// Registers a new monitor.
 /// </summary>
 /// <param name="monitor">Monitor</param>
 internal void RegisterMonitor(Monitor monitor)
 {
     this.Monitors.Add(monitor);
 }