SharpLibEar/ManagerEventAction.cs
author StephaneLenclud
Sun, 24 Jul 2016 14:22:56 +0200
changeset 213 77092f415c7c
child 214 4961ede27e0a
permissions -rw-r--r--
Reflection functions now working on all loaded assemblies.
Stephane@212
     1
//
Stephane@212
     2
Stephane@212
     3
Stephane@212
     4
using System;
Stephane@212
     5
using System.Collections.Generic;
Stephane@212
     6
using System.ComponentModel;
Stephane@212
     7
using System.Linq;
Stephane@212
     8
using System.Reflection;
Stephane@212
     9
using System.Runtime.Serialization;
Stephane@212
    10
using SharpLib.Utils;
Stephane@212
    11
Stephane@212
    12
namespace SharpLib.Ear
Stephane@212
    13
{
Stephane@212
    14
    [TypeConverter(typeof(TypeConverterJson<ManagerEventAction>))]
Stephane@212
    15
    [DataContract]
Stephane@212
    16
    public class ManagerEventAction
Stephane@212
    17
    {
Stephane@212
    18
        public static ManagerEventAction Current = null;
Stephane@212
    19
        public IDictionary<string, Type> ActionTypes;
Stephane@212
    20
        public IDictionary<string, Event> Events;
Stephane@212
    21
        [DataMember]
Stephane@212
    22
        public Dictionary<string, List<Action>> ActionsByEvents = new Dictionary<string, List<Action>>();
Stephane@212
    23
Stephane@212
    24
Stephane@212
    25
        public ManagerEventAction()
Stephane@212
    26
        {
Stephane@212
    27
            Init();
Stephane@212
    28
        }
Stephane@212
    29
Stephane@212
    30
        /// <summary>
Stephane@212
    31
        /// 
Stephane@212
    32
        /// </summary>
Stephane@212
    33
        public void Init()
Stephane@212
    34
        {
Stephane@212
    35
            //Create our list of supported actions
Stephane@212
    36
            ActionTypes = Utils.Reflection.GetConcreteClassesDerivedFromByName<Action>();
Stephane@212
    37
            //Create our list or support events
Stephane@212
    38
            Events = Utils.Reflection.GetConcreteClassesInstanceDerivedFromByName<Event>();
Stephane@212
    39
Stephane@212
    40
            if (ActionsByEvents == null)
Stephane@212
    41
            {
Stephane@212
    42
                
Stephane@212
    43
                ActionsByEvents = new Dictionary<string, List<Action>>();
Stephane@212
    44
            }
Stephane@212
    45
Stephane@212
    46
            //Hook in loaded actions with corresponding events
Stephane@212
    47
            foreach (string key in Events.Keys)
Stephane@212
    48
            {
Stephane@212
    49
                Event e = Events[key];
Stephane@212
    50
                if (ActionsByEvents.ContainsKey(key))
Stephane@212
    51
                {
Stephane@212
    52
                    //We have actions for that event, hook them in then
Stephane@212
    53
                    e.Actions = ActionsByEvents[key];
Stephane@212
    54
                }
Stephane@212
    55
                else
Stephane@212
    56
                {
Stephane@212
    57
                    //We do not have actions for that event yet, create empty action list
Stephane@212
    58
                    e.Actions = new List<Action>();
Stephane@212
    59
                    ActionsByEvents[key] = e.Actions;
Stephane@212
    60
                }
Stephane@212
    61
            }
Stephane@212
    62
        }
Stephane@212
    63
Stephane@212
    64
        public Event GetEvent<T>() where T : class
Stephane@212
    65
        {
Stephane@212
    66
            return Events[typeof(T).Name];
Stephane@212
    67
        }
Stephane@212
    68
Stephane@212
    69
    }
Stephane@212
    70
}