Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ExpressionServices Class

A transformation API used to convert environment aware expressions to an activity tree.

System.Object
  System.Activities.Expressions.ExpressionServices

Namespace:  System.Activities.Expressions
Assembly:  System.Activities (in System.Activities.dll)

public static class ExpressionServices

The ExpressionServices type exposes the following members.

  NameDescription
Public methodStatic memberConvert<TResult>Converts a workflow environment-aware expression to an activity tree.
Public methodStatic memberConvertReference<TResult>Converts a reference to a workflow environment-aware expression to an activity tree.
Public methodStatic memberTryConvert<TResult>Converts a workflow environment-aware expression to an activity tree.
Public methodStatic memberTryConvertReference<TResult>Converts a reference to a workflow environment-aware expression to an activity tree.
Top

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);

    }

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2015 Microsoft