gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:36:38 29 4
gpt4 key购买 nike

在为重新托管的工作流程设计器编写自定义事件时,它会给我一个错误,如果我没有所需参数,则需要一个值,因此我要么使用如下所示的 null ,要么使用“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/

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