ExpressionServices 类
定义
用于将环境感知表达式转换为活动树的转换 API。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
- 继承
-
ExpressionServices
示例
下面的代码示例调用 Convert 来计算位于索引 0 处的数组元素和位于索引 1 处的数组元素的和。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. 此第二个示例演示如何通过实例化 Add<TLeft,TRight,TResult> 表达式活动来计算和。This second example shows how to compute the sum by instantiating the Add<TLeft,TRight,TResult> expression activity. 这两个示例的功能相当,但正如您所看到的,第二种方法涉及到更多编码,并且不如调用 Convert 简单。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);
}
注解
此类中的转换方法将可能包含多个子表达式的指定 lambda 表达式转换为由活动层次结构组成的活动树。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.
ExpressionServices 中的转换方法设计为可以使用在工作流内部定义以及通过参数传入工作流的变量和常量。The conversion methods in ExpressionServices are designed to work with variables and constants defined inside the workflow, or passed into the workflow via arguments.
方法
| 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. |