コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Кроме как для удобного просмотра карты всех апи этот метод ни для чего не служит.
        /// </summary>
        public string AsString()
        {
            StringBuilder res = new StringBuilder();

            foreach (var item in _longNameAndInfo)
            {
                string newStr = item.Key;
                if (_longNameAndMethod.ContainsKey(item.Key))
                {
                    newStr += "(";
                    bool isFirst = true;
                    foreach (var parameter in _longNameAndMethod[item.Key].Parameters)
                    {
                        if (!isFirst)
                        {
                            newStr += ", ";
                        }
                        isFirst = false;
                        newStr += parameter.ParamType.Name + "  "
                                  + TextExtensions.ToUnderscoreCase(parameter.ParamName);
                    }
                    newStr += ")";
                }
                newStr += ";";
                if (item.Value.Description != null)
                {
                    newStr += $"  /*{item.Value.Description}*/";
                }
                res.AppendLine(newStr);
            }
            return(res.ToString());
        }
コード例 #3
0
 string ToCurrentNotation(string inputStr)
 {
     if (_underscoreNotationEnabled)
     {
         return(TextExtensions.ToUnderscoreCase(inputStr));
     }
     return(inputStr);
 }
コード例 #4
0
        /// <summary>
        /// Ищем свойства-категории. Все методы из типа свойства будут добавленны в reflection map с префиксом в виде имени свойства (учитывая погружение).
        /// </summary>
        /// <param name="t">Исследуемый тип, не путать с InspectedType, ведь с погружением в его поля это будет их тип, соответственно.</param>
        /// <param name="funcToGetLocalInstanceFromGlobal">Делегат для получения объекта с типом t из InspectedType.</param>
        void inspectCategoryProps(string prefix, string realNamePrefix, Type t, Func <object, object> funcToGetLocalInstanceFromGlobal)
        {
            foreach (var item in t.GetProperties())
            {
                var attr = item.GetCustomAttribute(typeof(CategoryProp_ReflectionMapAttribute)) as CategoryProp_ReflectionMapAttribute;
                if (attr != null)
                {
                    var newInfo = new Info_ReflectionMap()
                    {
                        DisplayName = prefix + (attr.DisplayName ?? TextExtensions.ToUnderscoreCase(item.Name)),
                        RealName    = realNamePrefix + item.Name,
                        Description = attr.Description
                    };

                    _longNameAndInfo.Add(
                        prefix + newInfo.DisplayName,
                        newInfo
                        );

                    var categoryGetter = item.GetMethod;
                    Func <object, object> new_funcToGetLocalInstanceFromGlobal = (globalInst) =>
                    {
                        var parentInst = funcToGetLocalInstanceFromGlobal(globalInst);
                        var res        = categoryGetter.Invoke(parentInst, new object[0]);
                        return(res);
                    };

                    var newPrefix         = prefix + newInfo.DisplayName + _splitter;
                    var newRealNamePrefix = realNamePrefix + newInfo.RealName + ".";
                    inspectMetods(
                        newPrefix,
                        newRealNamePrefix,
                        item.PropertyType,
                        new_funcToGetLocalInstanceFromGlobal
                        );

                    inspectSimpleProps(
                        newPrefix,
                        newRealNamePrefix,
                        item.PropertyType,
                        new_funcToGetLocalInstanceFromGlobal
                        );

                    inspectCategoryProps(
                        newPrefix,
                        newRealNamePrefix,
                        item.PropertyType,
                        new_funcToGetLocalInstanceFromGlobal
                        );
                }
            }
        }
コード例 #5
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);
        }
コード例 #6
0
        /// <summary>
        /// Ищем методы.
        /// </summary>
        /// <param name="t">Исследуемый тип, не путать с InspectedType, ведь с погружением в его поля это будет их тип, соответственно.</param>
        /// <param name="funcToGetLocalInstanceFromGlobal">Делегат для получения объекта с типом t из InspectedType.</param>
        void inspectMetods(string prefix, string realNamePrefix, Type t, Func <object, object> funcToGetLocalInstanceFromGlobal)
        {
            var methodInfoList = getAllMethods(t);

            foreach (var item in methodInfoList)
            {
                var attr = item.GetCustomAttribute(typeof(Method_ReflectionMapAttribute)) as Method_ReflectionMapAttribute;
                if (attr != null)
                {
                    var newInfo = new Info_ReflectionMap()
                    {
                        DisplayName = prefix + (attr.DisplayName ?? TextExtensions.ToUnderscoreCase(item.Name)),
                        RealName    = realNamePrefix + item.Name,
                        Description = attr.Description,
                    };

                    var newMethod = new Method_ReflectionMap()
                    {
                        Parameters = parameterInfoArrayToParamsArray(item.GetParameters()),
                        ReturnType = item.ReturnType
                    };
                    newMethod.InvokeAction = (globalInst, parameters) =>
                    {
                        var locInst = funcToGetLocalInstanceFromGlobal(globalInst);
                        return(item.Invoke(locInst, parameters));
                    };
                    checkIfTaskAndDoDirtWork(item, newMethod);


                    _longNameAndInfo.Add(
                        newInfo.DisplayName,
                        newInfo
                        );

                    _longNameAndMethod.Add(
                        newInfo.DisplayName,
                        newMethod
                        );
                }
            }
        }
コード例 #7
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());
        }
コード例 #8
0
        /// <summary>
        /// Ищем простые свойства (которые будут сконвертированы в методы с приставкой get и set.
        /// </summary>
        /// <param name="t">Исследуемый тип, не путать с InspectedType, ведь с погружением в его поля это будет их тип, соответственно.</param>
        /// <param name="funcToGetLocalInstanceFromGlobal">Делегат для получения объекта с типом t из InspectedType.</param>
        void inspectSimpleProps(string prefix, string realNamePrefix, Type t, Func <object, object> funcToGetLocalInstanceFromGlobal)
        {
            //Dictionary<string, MethodInfo_ReflectionMap> res =new Dictionary<string, MethodInfo_ReflectionMap>();
            foreach (var item in t.GetProperties())
            {
                var attr = item.GetCustomAttribute(typeof(SimpleProp_ReflectionMapAttribute)) as SimpleProp_ReflectionMapAttribute;
                if (attr != null)
                {
                    if (attr.CanGet && item.CanRead)
                    {
                        var newInfo = new Info_ReflectionMap()
                        {
                            DisplayName = prefix + ("get_" + (attr.DisplayName ?? TextExtensions.ToUnderscoreCase(item.Name))),
                            RealName    = realNamePrefix + item.Name,
                            Description = attr.Description
                        };

                        var newMethod = new Method_ReflectionMap()
                        {
                            Parameters = new Parameter[] { },
                            ReturnType = item.PropertyType,
                            Kind       = MethodKind.PropertyGetter
                        };

                        var getter = item.GetMethod;
                        newMethod.InvokeAction = (globalInst, parameters) =>
                        {
                            var locInst = funcToGetLocalInstanceFromGlobal(globalInst);
                            return(getter.Invoke(locInst, parameters));
                        };
                        checkIfTaskAndDoDirtWork(getter, newMethod);

                        _longNameAndInfo.Add(
                            newInfo.DisplayName,
                            newInfo
                            );


                        _longNameAndMethod.Add(
                            newInfo.DisplayName,
                            newMethod
                            );
                    }

                    if (attr.CanSet && item.CanWrite)
                    {
                        var newInfo = new Info_ReflectionMap()
                        {
                            DisplayName = prefix + ("set_" + (attr.DisplayName ?? TextExtensions.ToUnderscoreCase(item.Name))),
                            RealName    = realNamePrefix + "Set" + item.Name,
                            Description = attr.Description
                        };

                        var param = new Parameter
                        {
                            ParamType = item.PropertyType,
                            ParamName = "val"
                        };

                        var newMethod = new Method_ReflectionMap()
                        {
                            Parameters = new Parameter[] { param },
                            ReturnType = typeof(void),
                            Kind       = MethodKind.PropertySetter
                        };


                        var setter = item.SetMethod;
                        newMethod.InvokeAction = (globalInst, parameters) =>
                        {
                            var locInst = funcToGetLocalInstanceFromGlobal(globalInst);
                            return(setter.Invoke(locInst, parameters));
                        };
                        checkIfTaskAndDoDirtWork(setter, newMethod);

                        _longNameAndInfo.Add(
                            newInfo.DisplayName,
                            newInfo
                            );


                        _longNameAndMethod.Add(
                            newInfo.DisplayName,
                            newMethod
                            );
                    }
                }
            }
        }