gpt4 book ai didi

c# - Workflow Foundation 中自定义事件所需参数的默认值

转载 作者:行者123 更新时间:2023-12-02 04:39:19 24 4
gpt4 key购买 nike

在为重新托管的 Workflow Designer 编写自定义事件时,它给我一个错误,如果我没有必需的参数,则它需要一个值,所以我要么使用如下所示的空值,要么使用“ALL” ”。它不接受 null 或“ALL”或任何其他默认参数。请注意,我需要的参数是字符串类型。

[RequiredArgument]
[DefaultValue(null)]
[Description(@"The status of the job to perform")]
public InArgument<string> JobStatus { get; set; }

[RequiredArgument]
[DefaultValue("All")]
[Description(@"The status of the job to perform")]
public InArgument<string> JobStatus { get; set; }

运行工作流时的错误消息:

Workflow Console: Starting Workflow...

Could not start workflow.

Message: Failed to start workflow DynamicActivity.

Exception message: The following errors were encountered while processing the workflow tree: 'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error: Value for a required activity argument 'JobStatus' was not supplied.

Stack trace: at System.Activities.Validation.ActivityValidationServices.ThrowIfViolationsExist(IList`1 validationErrors) at System.Activities.Hosting.WorkflowInstance.ValidateWorkflow(WorkflowInstanceExtensionManager extensionManager) at System.Activities.Hosting.WorkflowInstance.RegisterExtensionManager(WorkflowInstanceExtensionManager extensionManager) at System.Activities.WorkflowApplication.EnsureInitialized() at System.Activities.WorkflowApplication.Enqueue(InstanceOperation operation, Boolean push) at System.Activities.WorkflowApplication.WaitForTurn(InstanceOperation operation, TimeSpan timeout) at System.Activities.WorkflowApplication.InternalRun(TimeSpan timeout, Boolean isUserRun) at System.Activities.WorkflowApplication.Run()

谢谢你的帮助

最佳答案

工作流运行时和工作流设计器都不会查看 DefaultValueAttribute。搜索属性需要反射,这可能会影响性能。此外,这不是该属性的目的。

无论如何,您可以使用默认值初始化变量。在构造函数上,例如:

public class MyCodeActivity : CodeActivity
{
public MyCodeActivity()
{
JobStatus = "All";
}
}

或者通过访问器强制一个值。像这样:

private InArgument<string> text = "All";

[RequiredArgument]
public InArgument<string> Text
{
get { return text ?? "All"; }
set { text = value; }
}

这些是不同的方法,请使用适合您想要的行为的方法。在这两种情况下,始终可以通过 XAML 修改值,因此对运行时进行小的检查可能会很好:

protected override void Execute(CodeActivityContext context)
{
string text = context.GetValue(this.Text);
if (text == null)
{
text = "All";
}

Console.WriteLine(text);
}

关于c# - Workflow Foundation 中自定义事件所需参数的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21241597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com