Exemplo n.º 1
0
        private void Init()
        {
            BotEventAttribute commandAttribute = mMethod.GetCustomAttribute <BotEventAttribute>();

            Name   = commandAttribute.EventName;
            mEvent = commandAttribute.Event;
        }
Exemplo n.º 2
0
            public EventManager(Bot owner, CallbackManager callbackManager)
            {
                Bot = owner;
                Log = owner.Log;

                Log.Info("Subscribing events...");

                MethodInfo subscribeMethod = null;

                foreach (MethodInfo m in callbackManager.GetType().GetMethods())
                {
                    if (m.Name == "Subscribe" && m.GetParameters().Length == 1)
                    {
                        subscribeMethod = m;
                    }
                }

                List <MethodInfo> methods = BotEventAttribute.GetMethodsWithinType(GetType());

                foreach (MethodInfo m in methods)
                {
                    ParameterInfo[] pars = m.GetParameters();
                    if (pars.Length == 0)
                    {
                        Log.Error("Method is missing parameters: " + m.Name);
                        continue;
                    }
                    if (pars.Length > 1)
                    {
                        Log.Error("Method has too many parameters: " + m.Name);
                        continue;
                    }

                    Type parType = pars.First().ParameterType;
                    if (!typeof(CallbackMsg).IsAssignableFrom(parType))
                    {
                        Log.Error("Method {0} has invalid parameter type: {1}", m.Name, parType.ToString());
                        continue;
                    }

                    MethodInfo genericSubscribe = subscribeMethod.MakeGenericMethod(parType);
                    object     del = CreateDelegateByParameter(parType, this, m);

                    // manager.Subscribe<parType>(del); // <-- essentially this
                    object subRes = genericSubscribe.Invoke(callbackManager, new object[] { del });

                    _subscriptions.Add(subRes as IDisposable);
                    Log.Debug("Subscribed callback event {0}.", m.Name);
                }

                Log.Success("{0} events subscribed.", _subscriptions.Count);
            }