表达式 (WF)

Windows Workflow Foundation (WF) 表达式是返回结果的任何活动。 所有表达式活动都间接派生自Activity<TResult>,其中包含一个名为OutArgumentResult活动返回值的属性。 WF 附带了多种表达式活动,从简单的VariableValue<T>VariableReference<T>活动(它们通过操作符活动提供对单个工作流变量的访问权限),到复杂的活动,如VisualBasicReference<TResult>VisualBasicValue<TResult>,这些活动提供了对 Visual Basic 语言完整广度的访问权限,从而产生结果。 可以通过从 CodeActivity<TResult>NativeActivity<TResult> 派生来创建其他表达式活动。

使用表达式

工作流设计器使用 VisualBasicValue<TResult>VisualBasicReference<TResult> 用于 Visual Basic 项目中的所有表达式,以及 CSharpValue<TResult>CSharpReference<TResult> C# 工作流项目中的表达式。

注释

.NET Framework 4.5 中引入了对工作流项目中 C# 表达式的支持。 有关详细信息,请参阅 C# 表达式

设计器生成的工作流保存在 XAML 中,其中表达式显示在方括号中,如以下示例所示。

<Sequence xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Sequence.Variables>
    <Variable x:TypeArguments="x:Int32" Default="1" Name="a" />
    <Variable x:TypeArguments="x:Int32" Default="2" Name="b" />
    <Variable x:TypeArguments="x:Int32" Default="3" Name="c" />
    <Variable x:TypeArguments="x:Int32" Default="0" Name="r" />
  </Sequence.Variables>
  <Assign>
    <Assign.To>
      <OutArgument x:TypeArguments="x:Int32">[r]</OutArgument>
    </Assign.To>
    <Assign.Value>
      <InArgument x:TypeArguments="x:Int32">[a + b + c]</InArgument>
    </Assign.Value>
  </Assign>
</Sequence>

在代码中定义工作流时,可以使用任何表达式活动。 以下示例显示了运算符活动组合的用法,以添加三个数字:

Variable<int> a = new Variable<int>("a", 1);
Variable<int> b = new Variable<int>("b", 2);
Variable<int> c = new Variable<int>("c", 3);
Variable<int> r = new Variable<int>("r", 0);

Sequence w = new Sequence
{
    Variables = { a, b, c, r },
    Activities =
    {
        new Assign {
            To = new OutArgument<int>(r),
            Value = new InArgument<int> {
                Expression = new Add<int, int, int> {
                    Left = new Add<int, int, int> {
                        Left = new InArgument<int>(a),
                        Right = new InArgument<int>(b)
                    },
                    Right = new InArgument<int>(c)
                }
            }
        }
    }
};

使用 C# lambda 表达式可以更紧凑地表达相同的工作流,如以下示例所示:

Variable<int> a = new Variable<int>("a", 1);
Variable<int> b = new Variable<int>("b", 2);
Variable<int> c = new Variable<int>("c", 3);
Variable<int> r = new Variable<int>("r", 0);

Sequence w = new Sequence
{
    Variables = { a, b, c, r },
    Activities =
    {
        new Assign {
            To = new OutArgument<int>(r),
            Value = new InArgument<int>((ctx) => a.Get(ctx) + b.Get(ctx) + c.Get(ctx))
        }
    }
};

使用自定义表达式活动扩展可用表达式

.NET Framework 4.6.1 中的表达式可扩展,允许创建其他表达式活动。 下面的示例演示一个返回三个整数值的总和的活动。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;

namespace ExpressionsDemo
{
    public sealed class AddThreeValues : CodeActivity<int>
    {
        public InArgument<int> Value1 { get; set; }
        public InArgument<int> Value2 { get; set; }
        public InArgument<int> Value3 { get; set; }

        protected override int Execute(CodeActivityContext context)
        {
            return Value1.Get(context) +
                   Value2.Get(context) +
                   Value3.Get(context);
        }
    }
}

使用此新活动,可以重写上一个工作流,该工作流添加了三个值,如以下示例所示:

Variable<int> a = new Variable<int>("a", 1);
Variable<int> b = new Variable<int>("b", 2);
Variable<int> c = new Variable<int>("c", 3);
Variable<int> r = new Variable<int>("r", 0);

Sequence w = new Sequence
{
    Variables = { a, b, c, r },
    Activities =
    {
        new Assign {
            To = new OutArgument<int>(r),
            Value = new InArgument<int> {
                Expression = new AddThreeValues() {
                    Value1 = new InArgument<int>(a),
                    Value2 = new InArgument<int>(b),
                    Value3 = new InArgument<int>(c)
                }
            }
        }
    }
};

有关在代码中使用表达式的详细信息,请参阅 使用命令性代码创作工作流、活动和表达式