Get Child Activity Subtree

时光怂恿深爱的人放手 提交于 2019-12-11 09:57:59

问题


I'm converting a legacy workflow system to WF4 so I have to jump through a couple hoops to make it match up with the api of our application. So I'll try to keep the problem explination as simple as possible. :)

I have a custom activity that takes a sequence as an argument and then executes it. Prior to executing it, the custom activity needs to traverse the sequence (and it's branches etc) looking for specific types of child activities - then it does some reporting on these specific child activities.

I know it is possible to traverse the child sub tree of an activity during Validation time when a Constraint can use a GetChildSubtree activiy, but this doesn't give me access to the list at runtime. I also know it's also possible to execute a similar call using ActivityValidationServices from the host application, but that won't work for my scenario either.

So what's the best way to get a list of the activities in the child subtree from within the execution method of a custom activity.?

Thanks in advance!

Marcus.


回答1:


You might want to take a look at WorkflowInspectionServices class which provides methods for working with the runtime metadata for an activity tree. In particular the GetActivities method.

GetActivities returns all the direct children of an activity, including activities, delegate handlers, variable defaults, and argument expressions. You can now write an extension method to return all activities including the inner branches:

public static IEnumerable<Activity> GetInnerActivities(this Activity activity)
{
    var children = WorkflowInspectionServices.GetActivities(activity);

    foreach (var child in children)
    {
        children = children.Concat(child.GetChildren());
    }

    return children;
}

Now get all activity's inner activities of a specified type:

activity.GetInnerActivities().OfType<MySpecificType>();


来源:https://stackoverflow.com/questions/12537412/get-child-activity-subtree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!