示例#1
0
        /// <summary>
        /// Build <see cref="CommandHandler"/> that represents <see cref="MethodInfo"/> on the instance <see cref="CommandCategory"/>.
        /// </summary>
        public void Build(CommandAttribute attribute, CommandCategory category, MethodInfo method)
        {
            permission      = attribute.Permission;
            methodContainer = new MethodContainer(category, method);

            HandleOptionalAttributes(attribute, method);

            ParameterInfo[] parameterInfo = method.GetParameters();
            BuildParameters(attribute, parameterInfo);
            BuildHelp(attribute, parameterInfo);
        }
示例#2
0
        private void BuildChildren()
        {
            var builder = ImmutableDictionary.CreateBuilder <string, ICommandHandler>(
                StringComparer.InvariantCultureIgnoreCase);

            // child handlers
            foreach (MethodInfo method in GetType().GetMethods())
            {
                CommandAttribute attribute = method.GetCustomAttribute <CommandAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                var handler = new CommandHandler();
                handler.Build(attribute, this, method);

                foreach (string command in attribute.Commands)
                {
                    builder.Add(command, handler);
                }
            }

            // child categories
            foreach (Type type in GetType().GetNestedTypes())
            {
                CommandAttribute attribute = type.GetCustomAttribute <CommandAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                CommandCategory category = (CommandCategory)Activator.CreateInstance(type);
                category.Build(attribute);

                foreach (string command in attribute.Commands)
                {
                    builder.Add(command, category);
                }
            }

            handlers = builder.ToImmutable();
        }
示例#3
0
        private void InitialiseHandlers()
        {
            log.Info("Initialising command handlers...");

            var builder = ImmutableDictionary.CreateBuilder <string, ICommandHandler>(
                StringComparer.InvariantCultureIgnoreCase);

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                CommandAttribute attribute = type.GetCustomAttribute <CommandAttribute>();
                if (attribute == null)
                {
                    continue;
                }

                // initialise only the parent, the children will be initialised from the parent itself
                if (type.IsNested)
                {
                    continue;
                }

                if (!typeof(CommandCategory).IsAssignableFrom(type))
                {
                    continue;
                }

                CommandCategory category = (CommandCategory)Activator.CreateInstance(type);
                category.Build(attribute);

                foreach (string command in attribute.Commands)
                {
                    builder.Add(command, category);
                }
            }

            handlers = builder.ToImmutable();
        }