internal static EventDict GenerateLookup() { var lookup = new EventDict(); foreach (Type type in Assembly.GetAssembly(typeof(BaseEvent)).GetTypes()) { if (!type.IsClass || type.IsAbstract || !type.IsSubclassOf(typeof(BaseEvent)) || type.IsEquivalentTo(typeof(UnknownEvent))) { continue; } var tmp = (BaseEvent)Activator.CreateInstance(type); var id = tmp.ID; if (lookup.ContainsKey(id)) { throw new Exception("ID already in lookup map"); } var lambda = Expression.Lambda <Func <BaseEvent> >( Expression.New(type), Array.Empty <ParameterExpression>() ).Compile(); lookup.Add(id, lambda); } return(lookup); }
private static IEvent CreateEvent(byte rawID) { var id = (EventID)rawID; if (!Lookup.ContainsKey(id)) { throw new IOException("Unknow event ID!"); } return(Lookup[id]()); }
private static EventDict GenerateLookup() { var lookup = new EventDict(); foreach (Type type in Assembly.GetAssembly(typeof(IEvent)).GetTypes()) { if (!type.IsClass || type.IsAbstract || type.IsInterface || !typeof(IEvent).IsAssignableFrom(type)) { continue; } var tmp = (IEvent)Activator.CreateInstance(type); var id = tmp.ID; if (lookup.ContainsKey(id)) { throw new Exception("ID already in lookup map"); } var lambda = Expression.Lambda <Func <IEvent> >( Expression.New(type), Array.Empty <ParameterExpression>() ).Compile(); lookup.Add(id, lambda); } return(lookup); }