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