This sample demonstrates how to implement an activity for which the arguments are defined by the activity consumer rather than the activity author. It does this by overriding the way the runtime constructs the activity’s metadata.
Prior to execution, the runtime builds a description of an activity by examining the public members of the activity type and automatically declaring arguments, variables, child activities, and activity delegates as part of an activity’s metadata. It does this to ensure correct construction of a workflow as well as to manage run-time relationships and lifetime rules. Typically an activity author defines the arguments of an activity by specifying public members on the activity type that derive from Argument. For each public member that derives from Argument, the runtime creates a RuntimeArgument and binds it to the user-provided argument set on that member. In some cases, however, the consumer of the activity provides some configuration that determines the set of arguments. An activity author overrides CacheMetadata to customize the way activity metadata is built, which includes the set of arguments associated with the activity.
This sample demonstrates how to build an argument list dynamically for an activity that invokes a method. The activity consumer specifies the type and the method name they want to invoke along with a collection of arguments to be passed to that method.
|The purpose of this sample is to demonstrate how to override CacheMetadata and how to use RuntimeArgument. There are several caveats with respect to the kinds of methods that this activity can invoke. For example, it does not work with generics or parameter arrays. The InvokeMethod activity that ships in.NET Framework handles these cases and more.|
MethodInvoke activity overrides CacheMetadata and begins by creating a RuntimeArgument to handle any result from the method invocation. It binds this RuntimeArgument to the publicly settable OutArgument named
MethodInvoke.Result is null, the runtime automatically populates it with an OutArgument configured with the default expression for its type. This behavior means an activity author never has to check whether an argument property is null.
Next, the CacheMetadata override determines the
MethodInfo it uses for invocation from the user-provided
DetermineMethodInfo method takes the CodeActivityMetadata parameter passed to the CacheMetadata override so that any configuration errors can be reported as validation errors. This is done by calling
MethodInfo has been set, the sample iterates over the
MethodInfo parameters. For each parameter, it creates a RuntimeArgument and binds it to the corresponding argument in the user-provided collection from the
Parameters property. Finally, the collection of RuntimeArguments is associated with the activity by calling
Note that argument resolution can be done using a RuntimeArgument, as in the case of
resultArgument or the user-provided argument, as in the case of the
To use this sample
Using Visual Studio 2010, open the DynamicArguments.sln file.
To build the solution, press CTRL+SHIFT+B.
To run the solution, press CTRL+F5.
The samples may already be installed on your computer. Check for the following (default) directory before continuing.