语言

ExpressionServices.ConvertReference<TResult> 方法

定义

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

public:
generic <typename TResult>
 static System::Activities::Activity<System::Activities::Location<TResult> ^> ^ ConvertReference(System::Linq::Expressions::Expression<Func<System::Activities::ActivityContext ^, TResult> ^> ^ expression);
public static System.Activities.Activity<System.Activities.Location<TResult>> ConvertReference<TResult>(System.Linq.Expressions.Expression<Func<System.Activities.ActivityContext,TResult>> expression);
static member ConvertReference : System.Linq.Expressions.Expression<Func<System.Activities.ActivityContext, 'Result>> -> System.Activities.Activity<System.Activities.Location<'Result>>
Public Shared Function ConvertReference(Of TResult) (expression As Expression(Of Func(Of ActivityContext, TResult))) As Activity(Of Location(Of TResult))

类型参数

TResult

表达式要转换为的类型。

参数

expression
Expression<Func<ActivityContext,TResult>>

要转换的表达式。

返回

Activity<Location<TResult>>

转换后的表达式。

示例

以下两个代码示例说明了如何使用 ConvertReference 和 Convert。 第一个ConvertReference代码示例在活动中用于Assign将 lambda 表达式转换为分配值的字符串属性。 接下来, Convert 调用 lambda 表达式将 lambda 表达式转换为在活动中打印到控制台的 WriteLine 字符串属性值。

// Define a struct with a property named AProperty.
struct StructWithProperty
{
    public string AProperty { get; set; }
}

public static void ConvertReferenceForValueTypePropertyReferenceSample()
{
    // Create a variable of type StructWithProperty to store the property.
    var swpvar = new Variable<StructWithProperty>("swpvar", new StructWithProperty());

    Activity myActivity = new Sequence
    {
        Variables = { swpvar },
        Activities =
        {
            // Create an Assign activity to assign a value to the AProperty property.
            new Assign<string>
            {
                To = ExpressionServices.ConvertReference<string>(ctx => swpvar.Get(ctx).AProperty),
                // Assign a string literal to AProperty.
                Value = "Hello",
            },
            // Print the new property value to the console.
            new WriteLine()
            {
                Text = ExpressionServices.Convert<string>(ctx => swpvar.Get(ctx).AProperty),
            }
        }
    };

    // Invoke the Sequence activity.
    WorkflowInvoker.Invoke(myActivity);
}

下面的代码示例与上一个代码示例类似,只不过要转换的表达式是对多维数组中的项的引用。

public static void ConvertReferenceForMultidimensionalArrayItemReferenceSample()
{
    // Create a variable to store a multidimensional array.
    var arrayvar = new Variable<int[,]>("arrayvar", new int[4, 5]);

    Activity myActivity = new Sequence
    {
        Variables = { arrayvar },
        Activities =
        {
            // Create an Assign activity to assign a value to the array item at index [1,2].
            new Assign<int>
            {
                To = ExpressionServices.ConvertReference<int>(ctx => arrayvar.Get(ctx)[1, 2]),
                // Assign an integer value to the array item at row 1 column 2.
                Value = 1,
            },
            // Print the array item value to the console.
            new WriteLine()
            {
                Text = ExpressionServices.Convert<string>(ctx => arrayvar.Get(ctx)[1, 2].ToString()),
            }
        }
    };

    // Invoke the Sequence activity.
    WorkflowInvoker.Invoke(myActivity);
}

注解

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

适用于