C# 如何在WF4中从工作流调用方法?

C# 如何在WF4中从工作流调用方法?,c#,workflow-foundation-4,C#,Workflow Foundation 4,我想从工作流中调用一个简单的方法(没有参数,返回void)。假设我有以下课程: public class TestClass { public void StartWorkflow() { var workflow = new Sequence { Activities = { new WriteLine { Text = "Before calling method.

我想从工作流中调用一个简单的方法(没有参数,返回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
,但这似乎不是我想要的。我还可以编写一个调用此方法的自定义活动,但我对这个场景特别感兴趣。这可能吗?

怎么样

公共类TestClass
{
公共无效StartWorkflow()
{
var工作流=新序列
{
活动=
{
新建WriteLine{Text=“在调用方法之前。”},
//这里我想调用ReusableMethod()方法。
新的InvokeMethod{MethodName=“ReusableMethod”,TargetType=typeof(TestClass)},
新WriteLine{Text=“在调用方法之后。”}
}
};
var wf=新工作流应用程序(工作流);
wf.Run();
var are=新的自动重置事件(假);
wf.Completed=新操作(arg=>are.Set());
WaitOne(5000);
}
公共静态void ReusableMethod()
{
Console.WriteLine(“内部方法”);
}
}
怎么样

公共类TestClass
{
公共无效StartWorkflow()
{
var工作流=新序列
{
活动=
{
新建WriteLine{Text=“在调用方法之前。”},
//这里我想调用ReusableMethod()方法。
新的InvokeMethod{MethodName=“ReusableMethod”,TargetType=typeof(TestClass)},
新WriteLine{Text=“在调用方法之后。”}
}
};
var wf=新工作流应用程序(工作流);
wf.Run();
var are=新的自动重置事件(假);
wf.Completed=新操作(arg=>are.Set());
WaitOne(5000);
}
公共静态void ReusableMethod()
{
Console.WriteLine(“内部方法”);
}
}

我假设您希望这是一个同步调用?不一定。在本例中,使用WorkflowInvoker并使其成为同步调用更有意义,但也可以是异步的。我假设您希望这是一个同步调用?不一定。在本例中,使用WorkflowInvoker并使其成为同步调用更有意义,但也可以是异步的。
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.");
    }
}