/// <summary>
        /// Создает словарь с названиями команд cli и соответствующими им методы.
        /// </summary>
        Dictionary <string, ResolvedCmdInfo> CreateReflectionDict()
        {
            var cmdNameAndMethod = new Dictionary <string, ResolvedCmdInfo>();

            foreach (MethodInfo item in GetType().GetMethods())
            {
                CmdInfoAttribute attr = item.GetCustomAttribute(typeof(CmdInfoAttribute)) as CmdInfoAttribute;
                if (attr != null)
                {
                    var name       = attr?.CmdName ?? TextExtensions.ToUnderscoreCase(item.Name);
                    var newCmdInfo = new ResolvedCmdInfo()
                    {
                        CmdName            = name,
                        Description        = attr.Description,
                        MethodInfo         = item,
                        OriginalMethodName = item.Name
                    };
                    cmdNameAndMethod.Add(
                        name,
                        newCmdInfo)
                    ;
                }
            }
            return(cmdNameAndMethod);
        }
        /// <summary>
        /// НЕ ПЕРЕДАВАТЬ НИКАКИХ ПАРАМЕТРОВ.
        /// </summary>
        CmdInfoAttribute GetCurrentMemberAttribute(string memberName)
        {
            CmdInfoAttribute attr = GetType().GetMethod(memberName)
                                    .GetCustomAttribute(typeof(CmdInfoAttribute)) as CmdInfoAttribute;

            if (attr == null)
            {
                throw new Exception("Can call this method only from methods with CmdInfoAttribute.");
            }
            return(attr);
        }
示例#3
0
        /// <summary>
        /// Создает словарь с названиями команд cli и соответствующими им методы.
        /// </summary>
        Dictionary <string, MethodInfo> CreateReflectionDict()
        {
            Dictionary <string, MethodInfo> cmdNameAndMethod = new Dictionary <string, MethodInfo>();

            foreach (MethodInfo item in this.GetType().GetMethods())
            {
                CmdInfoAttribute attr = item.GetCustomAttribute(typeof(CmdInfoAttribute)) as CmdInfoAttribute;
                if (attr != null)
                {
                    //TODO: Remove attribute crunch.
                    attr.CmdName = attr?.CmdName ?? TextExtensions.ToUnderscoreCase(item.Name);
                    cmdNameAndMethod.Add(
                        attr.CmdName,
                        item)
                    ;
                }
            }
            return(cmdNameAndMethod);
        }
示例#4
0
        public void HelpCmd()
        {
            StringBuilder res = new StringBuilder("\tВы можете передавать параметры в метод, разделяя из через '/'.\n" +
                                                  "\tИспользуйте параметр /auto для автоматического выполнения команд.\n\n");

            foreach (MethodInfo item in this.GetType().GetMethods())
            {
                CmdInfoAttribute attr = item.GetCustomAttribute(typeof(CmdInfoAttribute)) as CmdInfoAttribute;
                if (attr != null && attr.CmdName != "help")
                {
                    //TODO: Remove attribute crunch.
                    attr.CmdName = attr?.CmdName ?? TextExtensions.ToUnderscoreCase(item.Name);

                    string newStr  = "\t" + attr.CmdName + " - " + item.Name + "(";
                    bool   isFirst = true;

                    foreach (var parameter in item.GetParameters())
                    {
                        if (!isFirst)
                        {
                            newStr += ", ";
                        }
                        isFirst = false;
                        newStr += parameter.ParameterType.Name + "  " + parameter.Name;
                    }
                    newStr += ");";

                    if (attr.Description != null)
                    {
                        newStr += $"  /*{attr.Description}*/";
                    }
                    res.AppendLine(newStr);
                }
            }
            Cmd.Write(res.ToString());
        }