代码之家  ›  专栏  ›  技术社区  ›  Ronald Wildenberg

如何从WF4中的工作流调用方法?

  •  1
  • Ronald Wildenberg  · 技术社区  · 14 年前

    我想在工作流中调用一个简单的方法(没有参数,返回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 回复  |  直到 14 年前
        1
  •  5
  •   Kevin Driedger    14 年前

    怎么样 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.");
        }
    }