Skip to content

支持结构类型的返回结果

从章节支持返回结果中,我们已经了解到,通过给属性标注ResultToPropertyAttribute, 可以在命令执行后生成一个或多个返回结果,以便后续命令使用。

如果希望生成复杂的对象类型返回结果,或者生成数组类型的返回结果,通过标注ResultToPropertyAttribute,也是可以实现的,但是再后续的属性提示中,用户无法快捷的了解返回的对象有哪些子属性。只能通过点操作符硬取值。

例如一下代码

csharp
using GrapeCity.Forguncy.Commands;
using System.ComponentModel;

namespace MyPlugin
{
    public class MyPluginCommand : Command
    {
        [ResultToProperty]
        [DisplayName("学生信息")]
        public string StudentInfo { get; set; }
    }
}

对象类型返回值

假设,命令执行后,会返回学生对象。包含了姓名和年龄属性,但是再后续命令使用结果时只会提示,学生信息变量,而如果需要获取子属性值,必须用户手动,准确输入,用户体验较差。
1691109739646-978bd1f0-0428-4fba-bc7b-aaa949f0176b.png

如果希望同时提示,子属性,可以通过实现 IServerCommandParamGenerator 接口实现

csharp
using GrapeCity.Forguncy.Commands;
using System.Collections.Generic;
using System.ComponentModel;

namespace MyPlugin
{
    public class MyPluginCommand : Command, IServerCommandParamGenerator
    {
        [ResultToProperty]
        [DisplayName("学生信息")]
        public string StudentInfo { get; set; }

        public IEnumerable<GenerateParam> GetGenerateParams()
        {
            yield return new GenerateObjectParam()
            {
                ParamName = this.StudentInfo,
                Description = "查询学生的详细信息结果",
                ParamScope = CommandScope.All,
                SubPropertiesDescription = new Dictionary<string, string>() {
                    { "Name","学生姓名"},
                    { "Age","学生年龄"},
                    { "Address.Country","住址(国家)"},
                    { "Address.City","住址(城市)"}
                }
            };
        }
    }
}

效果如下

1691112315384-f1b451e0-a7e6-4eb2-80eb-41d2879502bb.png

数组类型返回值

如果返回值是列表类型,同样可以通过实现IServerCommandParamGenerator解决

csharp
using GrapeCity.Forguncy.Commands;
using System.Collections.Generic;
using System.ComponentModel;

namespace MyPlugin
{
    public class MyPluginCommand : Command, IServerCommandParamGenerator
    {
        [ResultToProperty]
        [DisplayName("学生信息")]
        public string StudentInfo { get; set; }

        public IEnumerable<GenerateParam> GetGenerateParams()
        {
            yield return new GenerateListParam()
            {
                ParamName = this.StudentInfo,
                Description = "查询学生结果列表",
                ParamScope = CommandScope.All,
                ItemProperties = new List<string>() {
                    { "Name"},
                    { "Age"},
                    { "Address.Country"},
                    { "Address.City"}
                }
            };
        }
    }
}

效果如下

在普通命令中使用时

1691112853430-05e6e07f-2e74-4ab9-bbe4-a0a7d0c3c8ca.png

在循环命令的子命令中使用时

1691112968386-b37c76b8-c886-463e-a6ff-32c43afa13ac.png

更新: 2023-08-04 09:36:19
原文: https://www.yuque.com/robert-bh51n/ea8l6c/ramga3tvktwaehza