Пример #1
0
        /// <summary>
        /// 获取所有参数的信息列表
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="tryLoadData">是否尝试载入数据(参数必须实现 IFunctionParameterLoadDataBase 接口)</param>
        /// <returns></returns>
        public async Task <List <FunctionParameterInfo> > GetFunctionParameterInfoAsync(IServiceProvider serviceProvider, bool tryLoadData)
        {
            var obj = GenerateParameterInstance();

            //预载入参数
            if (tryLoadData && obj is IFunctionParameterLoadDataBase loadDataParam)
            {
                await loadDataParam.LoadData(serviceProvider);//载入参数
            }

            var           props                 = FunctionParameterType.GetProperties();
            ParameterType parameterType         = ParameterType.Text;
            List <FunctionParameterInfo> result = new List <FunctionParameterInfo>();

            foreach (var prop in props)
            {
                List <string> selectionItems = null;
                //判断是否存在选项
                if (prop.PropertyType.IsArray)
                {
                    var selection = prop.GetValue(obj, null);
                    if (selection == null)
                    {
                        continue;//此参数不加入
                    }

                    selectionItems = new List <string>();
                    parameterType  = ParameterType.SingleSelection;//TODO:根据其他条件(如创建一个新的Attribute)判断多选
                    foreach (var item in (Array)selection)
                    {
                        selectionItems.Add(item.ToString());
                    }
                }

                var    name            = prop.Name;
                string title           = null;
                string description     = null;
                var    isRequired      = prop.GetCustomAttribute <RequiredAttribute>() != null;
                var    descriptionAttr = prop.GetCustomAttribute <DescriptionAttribute>();
                if (descriptionAttr != null && descriptionAttr.Description != null)
                {
                    //分割:名称||说明
                    var descriptionAttrArr = descriptionAttr.Description.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                    title = descriptionAttrArr[0];
                    if (descriptionAttrArr.Length > 1)
                    {
                        description = descriptionAttrArr[1];
                    }
                }
                var systemType = prop.PropertyType.Name;

                object value = null;
                try
                {
                    value = prop.GetValue(obj);
                }
                catch (Exception ex)
                {
                    SenparcTrace.BaseExceptionLog(ex);
                }

                var functionParamInfo = new FunctionParameterInfo(name, title, description, isRequired, systemType, parameterType,
                                                                  selectionItems?.ToArray(), value);
                result.Add(functionParamInfo);
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 获取所有参数的信息列表
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="tryLoadData">是否尝试载入数据(参数必须实现 IFunctionParameterLoadDataBase 接口)</param>
        /// <returns></returns>
        public async Task <List <FunctionParameterInfo> > GetFunctionParameterInfoAsync(IServiceProvider serviceProvider, bool tryLoadData)
        {
            var obj = GenerateParameterInstance();

            //预载入参数
            if (tryLoadData && obj is IFunctionParameterLoadDataBase loadDataParam)
            {
                await loadDataParam.LoadData(serviceProvider);//载入参数
            }

            var           props                 = FunctionParameterType.GetProperties();
            ParameterType parameterType         = ParameterType.Text;
            List <FunctionParameterInfo> result = new List <FunctionParameterInfo>();

            foreach (var prop in props)
            {
                SelectionList selectionList = null;
                parameterType = ParameterType.Text;//默认为文本内容
                //判断是否存在选项
                if (prop.PropertyType == typeof(SelectionList))
                {
                    var selections = prop.GetValue(obj, null) as SelectionList;
                    switch (selections.SelectionType)
                    {
                    case SelectionType.DropDownList:
                        parameterType = ParameterType.DropDownList;
                        break;

                    case SelectionType.CheckBoxList:
                        parameterType = ParameterType.CheckBoxList;
                        break;

                    default:
                        //TODO: throw
                        break;
                    }
                    selectionList = selections;
                }

                var    name            = prop.Name;
                string title           = null;
                string description     = null;
                var    isRequired      = prop.GetCustomAttribute <RequiredAttribute>() != null;
                var    descriptionAttr = prop.GetCustomAttribute <DescriptionAttribute>();
                if (descriptionAttr != null && descriptionAttr.Description != null)
                {
                    //分割:名称||说明
                    var descriptionAttrArr = descriptionAttr.Description.Split(new[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
                    title = descriptionAttrArr[0];
                    if (descriptionAttrArr.Length > 1)
                    {
                        description = descriptionAttrArr[1];
                    }
                }
                var systemType = prop.PropertyType.Name;

                object value = null;
                try
                {
                    value = prop.GetValue(obj);
                }
                catch (Exception ex)
                {
                    SenparcTrace.BaseExceptionLog(ex);
                }

                var functionParamInfo = new FunctionParameterInfo(name, title, description, isRequired, systemType, parameterType,
                                                                  selectionList ?? new SelectionList(SelectionType.Unknown), value);
                result.Add(functionParamInfo);
            }
            return(result);
        }