using System; using System.Collections.Generic; using System.Reflection; using Common.Base; using Common.Event; namespace Model { public class EventComponent: Component where T : AEventAttribute { private Dictionary> events; public void Load(Assembly assembly) { this.events = new Dictionary>(); var types = assembly.GetTypes(); foreach (var t in types) { object[] attrs = t.GetCustomAttributes(typeof (T), false); if (attrs.Length == 0) { continue; } object obj = Activator.CreateInstance(t); IEvent iEvent = obj as IEvent; if (iEvent == null) { throw new Exception(string.Format("event not inherit IEvent interface: {0}", obj.GetType().FullName)); } AEventAttribute iEventAttribute = (AEventAttribute) attrs[0]; if (!this.events.ContainsKey(iEventAttribute.Type)) { this.events.Add(iEventAttribute.Type, new List()); } this.events[iEventAttribute.Type].Add(iEvent); } } public void Trigger(int type, Env env) { List iEventDict = null; if (!this.events.TryGetValue(type, out iEventDict)) { return; } foreach (var iEvent in iEventDict) { iEvent.Trigger(env); } } } }