Ejemplo n.º 1
0
        private object[] ConvertParameters(string args, SocketMessage msg)
        {
            var implParams = _method.GetParameters();
            var retParams  = new object[implParams.Length];

            string[] split           = (args ?? string.Empty).Split(' ');
            int      argIdx          = 0;
            bool     cannotParseMore = false;

            for (int i = 0; i < implParams.Length; i++)
            {
                ParameterInfo ip     = implParams[i];
                Type          ipType = ip.ParameterType;
                if (ip.CustomAttributes.Any(x => x.AttributeType == typeof(DIAttribute)))
                {
                    retParams[i] = ipType.IsInstanceOfType(msg) ? msg : _dep.Get(ipType);
                }
                else
                {
                    if (cannotParseMore)
                    {
                        throw new Exception("Continued parsing after being told not to (multiple varlens?)");
                    }

                    if (ParameterParser.IsVarLen(ipType))   // this check relies on a check on multiple varlens being done before (see ctor)
                    {
                        if (!ParseParameter(ipType, string.Join(" ", split.Skip(argIdx++)), out retParams[i]))
                        {
                            throw new ArgumentException($"Failed to parse varlen argument {argIdx} :(");
                        }
                        cannotParseMore = true;
                    }
                    else if (!ParseParameter(ipType, split[argIdx++], out retParams[i]))
                    {
                        throw new ArgumentException($"Failed to parse argument {argIdx} :(");
                    }
                }
            }

            return(retParams);
        }
Ejemplo n.º 2
0
        internal MethodCommand(CommandAttribute cmdAttr, MethodInfo method, DependencyContainer dep) : base(cmdAttr.Command, cmdAttr.Description, cmdAttr.MinPermission ?? 0)
        {
            _method = method;
            _dep    = dep;

            Debug.Assert(_method != null);

            if (!_method.IsStatic)
            {
                throw new ArgumentException($"Tried to create a {nameof(MethodCommand)} using a non-static method.", nameof(_method));
            }

            _cmdParams       = _method.GetParameters().Where(x => x.CustomAttributes.All(y => y.AttributeType != typeof(DIAttribute))).Select(x => x.ParameterType).ToArray();
            _paramCount      = _cmdParams.Length;
            _endsOnVarLength = _paramCount > 0 && ParameterParser.IsVarLen(_cmdParams.Last());

            if (_cmdParams.Count(ParameterParser.IsVarLen) > 1)
            {
                throw new NotSupportedException("Found more than 2 strings in this command.");
            }
        }