语言

ExpressionServices 类

定义

用于将环境感知表达式转换为活动树的转换 API。

public ref class ExpressionServices abstract sealed
public static class ExpressionServices
type ExpressionServices = class
Public Class ExpressionServices
继承
ExpressionServices

示例

下面的代码示例调用 Convert 计算索引 0 处的数组元素和索引 1 处的数组元素的总和。 接下来,生成的总和分配给变量,并打印到控制台。

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

}

以下代码示例用于比较目的。 第二个示例演示如何通过实例化 Add<TLeft,TRight,TResult> 表达式活动来计算总和。 这两个示例在功能上是等效的,但正如你所看到的第二种方法涉及更多的编码,并且不像调用 Convert那样简单。 因此,建议使用第一个示例。

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

}

注解

此类中的转换方法将指定的 lambda 表达式(可以包含多个子表达式)转换为由活动层次结构组成的活动树。 强烈建议使用这些转换方法,而不是直接实例化表达式活动,因为它们提供更高级别的抽象并使你能够更直观地实现工作流。 有关详细信息,请参阅示例。

中的 ExpressionServices 转换方法旨在处理工作流中定义的变量和常量,或通过参数传递到工作流中。

方法

名称 说明
Convert<TResult>(Expression<Func<ActivityContext,TResult>>)

将工作流环境感知表达式转换为活动树。

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

将对工作流环境感知表达式的引用转换为活动树。

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

将工作流环境感知表达式转换为活动树。

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

将对工作流环境感知表达式的引用转换为活动树。

适用于