Define and consume activity delegates in the Workflow Designer
.NET Framework 4.5 includes an out-of-box designer for the InvokeDelegate activity. This designer can be used to assign delegates to the activity that derive from ActivityDelegate, such as ActivityAction or ActivityFunc<TResult>.
Define an activity delegate
Create a new Workflow Console Application project.
Note
If you don't see the Workflow project templates, first install the Windows Workflow Foundation component of Visual Studio. For detailed instructions, see Install Windows Workflow Foundation.
Right-click on the project in Solution Explorer and select Add > New Item. Select the Workflow category, and then select the Activity item template. Name the new activity MyForEach.xaml and then select OK.
The activity opens in the workflow designer.
In the Workflow Designer, click the Arguments tab.
Click Create Argument. Name the new argument Items.
In the Argument type column, select Array of [T].
In the type browser, select Object and then select OK.
Click Create Argument again. Name the new argument Body. In the Direction column for the new argument, select Property.
In the Argument Type column, select Browse for types
In the type browser, enter ActivityAction in the Type Name field. Select ActivityAction<T> in the tree view. Select Object in the dropdown that appears to assign the type ActivityAction<Object> to the argument.
Drag a While activity from the Control Flow section of the toolbox to the designer surface.
Select the While activity, and select the Variables tab.
Select Create Variable. Name the new variable Index.
In the Variable type column, select Int32. Leave the Scope as While, and the Default column blank.
Set the Condition property of the While activity to index < Items.Length;.
Drag an InvokeDelegate activity from the Primitives section of the toolbox to the Body of the While activity.
Select Body in the delegate drop-down.
In the Properties grid for the InvokeDelegate activity, click the ... button in the Delegate Arguments property.
In the Value column of the argument named Argument, enter Items[Index]. Click Ok to close the DelegateArguments dialog.
Drag an Assign activity onto the horizontal line underneath the InvokeDelegate activity. The Assign activity is created, and a Sequence activity is created automatically to contain the two activities in the Body section of the MyForEach activity. The sequence is needed since the Body section can only contain a single activity. Automatically creating a new Sequence activity is a new feature of .NET Framework 4.5.
Set the To property of the Assign activity to index. Set the Value property of the Assign activity to index+1.
The custom MyForEach activity invokes an arbitrary activity once for each value passed into it through the Items collection, with the values in the collection as inputs for the activity.
Use the custom activity in a workflow
Build the project by pressing Ctrl+Shift+B.
In Solution Explorer, open Workflow1.xaml in the designer.
Drag a MyForEach activity from the toolbox to the designer surface. The activity is in a section of the toolbox with the same name as the project.
Set the Items property of the MyForEach activity to new Object[] {1, "abc"}.
Drag a WriteLine activity from the Primitives section of the toolbox to the Delegate:Body section of the MyForEach activity.
Set the Text property of the WriteLine activity to Argument.ToString().
When the workflow is executed, the console shows the following output:
1 abc