コード例 #1
0
        public CommandInfo(MethodInfo method)
        {
            _method         = method;
            _parameters     = method.GetParameters();
            _parameterDict  = _parameters.ToDictionary(p => p.Name);
            _requiredParams = _parameters.Count(p => !p.IsOptional);

            _moduleFactory = DependencyUtility.CreateFactory(method.DeclaringType);

            if (method.ReturnType != typeof(Task) &&
                method.ReturnType.GetGenericTypeDefinition() != typeof(Task <>))
            {
                throw new ArgumentException($"{method} is not asynchronous.");
            }

            // build name regex
            _attribute = method.GetCustomAttribute <CommandAttribute>();

            if (_attribute == null)
            {
                throw new ArgumentException($"{method} is not a command.");
            }

            _nameRegex = new Regex(BuildNamePattern(method, _attribute), _options);

            // build parameter regex
            var bindingExpression  = method.GetCustomAttribute <BindingAttribute>()?.Expression;
            var requiredParameters = _parameterDict.Where(x => !x.Value.IsOptional).Select(x => x.Key);

            if (bindingExpression == null && _parameters.Any(p => !p.IsOptional))
            {
                bindingExpression = $"[{string.Join("] [", requiredParameters)}]";
            }

            _parameterRegex = new Regex(BuildParameterPattern(bindingExpression ?? ""), _options);

            // build optional parameter regex
            _optionRegex = new Regex(BuildOptionPattern(_parameters.Where(p => p.IsOptional).ToArray()), _options);
        }