You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为InArgument<string>定义Get/Set方法时Get的ActivityContext参数应传什么

System.Activities.ActivityContext参数传值规则

ActivityContext是Windows Workflow Foundation (WF) 运行时的内置上下文对象,不需要你手动构造或主动传入固定值,使用场景对应两种取值方式:

  • 如果你是在自定义活动的重写执行方法内访问InArgument<string>:直接使用WF运行时自动注入到Execute方法的上下文参数即可,代码示例如下:
public class CustomStringProcessActivity : CodeActivity
{
    // 你定义的InArgument属性
    public InArgument<string> InputText { get; set; }
    public OutArgument<string> OutputText { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        // 直接用运行时传入的context调用Get方法
        string input = InputText.Get(context);
        // 业务处理逻辑
        string processed = input?.Trim() ?? string.Empty;
        // Set方法同样用这个context参数
        OutputText.Set(context, processed);
    }
}
  • 如果你是在工作流宿主代码中操作InArgument:不要调用实例的Get方法,只需要在启动工作流实例时传入输入参数字典,WF运行时会自动完成参数绑定:
var inputParams = new Dictionary<string, object>
{
    { "InputText", "要传入的字符串值" }
};
// 启动工作流时传入参数即可
WorkflowInvoker.Invoke(new CustomStringProcessActivity(), inputParams);

注意:ActivityContext的构造函数是WF内部级别的,你无法在外部代码中手动实例化合法可用的上下文对象,所有可正常使用的实例都由WF运行时在活动执行的对应生命周期生成。

内容的提问来源于stack exchange,提问作者Jan021981

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 08:15:01