Editar

Partilhar via


ExpressionServices Class

Definition

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

public ref class ExpressionServices abstract sealed
public static class ExpressionServices
type ExpressionServices = class
Public Class ExpressionServices
Inheritance
ExpressionServices

Examples

The following code example calls Convert 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. 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);  

}  

Remarks

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.

Methods

Convert<TResult>(Expression<Func<ActivityContext,TResult>>)

Converts a workflow environment-aware expression to an activity tree.

ConvertReference<TResult>(Expression<Func<ActivityContext,TResult>>)

Converts a reference to a workflow environment-aware expression to an activity tree.

TryConvert<TResult>(Expression<Func<ActivityContext,TResult>>, Activity<TResult>)

Converts a workflow environment-aware expression to an activity tree.

TryConvertReference<TResult>(Expression<Func<ActivityContext,TResult>>, Activity<Location<TResult>>)

Converts a reference to a workflow environment-aware expression to an activity tree.

Applies to