共用方式為


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

已轉換的運算式。

範例

下列兩個程式碼範例示範 ConvertReferenceConvert 的用法。 第一個程式碼範例在 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 中的轉換方法是設計成使用在工作流程內部定義的變數和常數,或是經由引數傳入工作流程的變數和常數。

適用於