Page tree
Skip to end of metadata
Go to start of metadata

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

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

例如以下代码:

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

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

对象类型返回值

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

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

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","住址(城市)"}
                }
            };
        }
    }
}

效果如下:


数组类型返回值

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

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"}
                }
            };
        }
    }
}


效果如下。

在普通命令中使用时:



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


  • No labels