如何从WF 4中的工作流程调用方法?

时间:2010-08-17 13:57:17

标签: c# workflow-foundation-4

我想从工作流中调用一个简单的方法(没有参数,返回void)。假设我有以下课程:

public class TestClass
{
    public void StartWorkflow()
    {
        var workflow = new Sequence
        {
            Activities =
            {
                new WriteLine { Text = "Before calling method." },
                // Here I would like to call the method ReusableMethod().
                new WriteLine { Text = "After calling method." }
            }
        }
        new WorkflowApplication(workflow).Run();
    }

    public void ReusableMethod()
    {
        Console.WriteLine("Inside method.");
    }
}

如何在工作流程中拨打ReusableMethod?我在看InvokeAction,但这似乎不是我想要的。我还可以编写一个调用此方法的自定义活动,但我对此方案特别感兴趣。这可能吗?

1 个答案:

答案 0 :(得分:5)

InvokeMethod怎么样?

public class TestClass
{
    public void StartWorkflow()
    {
        var workflow = new Sequence
                        {
                            Activities =
                                {
                                    new WriteLine {Text = "Before calling method."},
                                    // Here I would like to call the method ReusableMethod().
                                    new InvokeMethod {MethodName="ReusableMethod", TargetType = typeof(TestClass)},
                                    new WriteLine {Text = "After calling method."}
                                }
                        };
        var wf = new WorkflowApplication(workflow);
        wf.Run();
        var are = new AutoResetEvent(false);
        wf.Completed = new Action<WorkflowApplicationCompletedEventArgs>(arg => are.Set());
        are.WaitOne(5000);
    }

    public static void ReusableMethod()
    {
        Console.WriteLine("Inside method.");
    }
}