ExpressionServices.ConvertReference<TResult> 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将对工作流环境感知表达式的引用转换为活动树。
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>>
所转换的表达式。
返回
转换后的表达式。
示例
下面的两个代码示例阐释 ConvertReference 和 Convert 的使用。 第一个代码示例将在 ConvertReference 活动中使用 Assign
将 lambda 表达式转换为分配了值的字符串属性。 接着,调用 Convert 将 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 中的转换方法设计为可以使用在工作流内部定义以及通过参数传入工作流的变量和常量。