ExpressionServices Class
A transformation API used to convert environment aware expressions to an activity tree.
Assembly: System.Activities (in System.Activities.dll)
The ExpressionServices type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | Convert<TResult> | Converts a workflow environment-aware expression to an activity tree. |
![]() ![]() | ConvertReference<TResult> | Converts a reference to a workflow environment-aware expression to an activity tree. |
![]() ![]() | TryConvert<TResult> | Converts a workflow environment-aware expression to an activity tree. |
![]() ![]() | TryConvertReference<TResult> | Converts a reference to a workflow environment-aware expression to an activity tree. |
The conversion methods in this class transform the specified lambda expressions, which can contain multiple sub-expressions, into activity trees composed of a hierarchy of activities. It is strongly recommended to use these conversion methods instead of instantiating expression activities directly because they provide a higher level of abstraction and enable you to implement your workflow more intuitively. See the examples for more information.
The conversion methods in ExpressionServices are designed to work with variables and constants defined inside the workflow, or passed into the workflow via arguments.
The following code example calls Convert<TResult>(Expression<Func<ActivityContext, TResult>>) to compute the sum of the array element at index 0 and the array element at index 1. Next, the resulting sum is assigned to a variable and is printed to the console.
public static void ComputeSumWithConvert()
{
var arrayvar = new Variable<int[]>("arrayvar", new int[] { 1, 2 });
var intvar = new Variable<int>("intvar");
// Use ExpressionServices.Convert() to convert the composite lambda expression
// that represents the sum of array elements at index 0 and 1.
Activity<int> activity1 = ExpressionServices.Convert<int>(ctx => arrayvar.Get(ctx)[0] + arrayvar.Get(ctx)[1]);
Activity seq = new Sequence
{
Variables = { arrayvar, intvar },
Activities =
{
// Get the sum value.
new Assign<int>
{
To = intvar,
Value = activity1,
},
// Print the sum value of 3 to the console.
new WriteLine
{
Text = new InArgument<string>(ctx => intvar.Get(ctx).ToString()),
},
}
};
WorkflowInvoker.Invoke(seq);
}
The following code example is provided for comparison purposes. This second example shows how to compute the sum by instantiating the Add<TLeft, TRight, TResult> expression activity. The two examples are functionally equivalent but as you can see the second approach involves more coding and is not as straightforward as calling Convert<TResult>(Expression<Func<ActivityContext, TResult>>). Therefore the first example is recommended.
public static void ComputeSumWithExpressionActivity()
{
var arrayvar = new Variable<int[]>("arrayvar", new int[] { 1, 2 });
var intvar = new Variable<int>("intvar");
// Create an Add activity to compute the sum of array elements at index 0 and 1.
Activity<int> activity1 = new Add<int, int, int>
{
Left = new ArrayItemValue<int>
{
Array = arrayvar,
Index = 0,
},
Right = new ArrayItemValue<int>
{
Array = arrayvar,
Index = 1,
}
};
Activity seq = new Sequence
{
Variables = { arrayvar, intvar },
Activities =
{
// Get the sum value.
new Assign<int>
{
To = intvar,
Value = activity1,
},
// Print the sum value of 3 to the console.
new WriteLine
{
Text = new InArgument<string>(ctx => intvar.Get(ctx).ToString()),
},
}
};
WorkflowInvoker.Invoke(seq);
}
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
