Skip to content

支持返回结果

命令执行后,可以把命令的执行结果保持到变量里,以便后续的命令或逻辑使用。

可以通过实现ResultToPropertyAttribute来实现此效果

示例代码,注意,标注 ResultToProperty 的属性类型必须是 string. 推荐给属性添加默认值,以方便用户使用

csharp
    public class MyPluginServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [FormulaProperty]
        [DisplayName("加数1")]
        public object AddNumber1 { get; set; }

        [FormulaProperty]
        [DisplayName("加数2")]
        public object AddNumber2 { get; set; }

        [ResultToProperty]
        [DisplayName("相加结果")]
        public string ResultTo { get; set; } = "结果";

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var add1 = await dataContext.EvaluateFormulaAsync(AddNumber1);
            var add2 = await dataContext.EvaluateFormulaAsync(AddNumber2);

            double.TryParse(add1?.ToString(), out var add1Number);
            double.TryParse(add2?.ToString(), out var add2Number);

            dataContext.Parameters[ResultTo] = add1Number + add2Number;

            return new ExecuteResult();
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }

设计器中的效果
1670343678794-72552be3-e5ad-461f-8d2c-882331c6c5dd.png

在后续命令编辑公式时,设置的变量可以直接在公式中使用

1670343697904-9ff18290-0583-43b0-84c0-c7fea603608f.png

更新: 2024-03-01 13:36:44
原文: https://www.yuque.com/robert-bh51n/ea8l6c/pz2oe016cno3sd5w