简体   繁体   English

如何在代码中获取OutArgument的强类型值?

[英]How do I get the strongly-typed value of an OutArgument in code?

Given an Activity (created via the designer) that has several OutArgument properties, is it possible to get their strongly-typed value from a property after invoking the workflow? 给定一个具有多个OutArgument属性的Activity (通过设计器创建),是否可以在调用工作流之后从一个属性中获取其强类型值?

The code looks like this: 代码如下:

// generated class
public partial class ActivityFoo : System.Activities.Activity....
{
    ....
    public System.Activities.OutArgument<decimal> Bar { ... }
    public System.Activities.OutArgument<string> Baz { ... }
}

// my class
var activity = new ActivityFoo();
var result = WorkflowInvoker.Invoke(activity);

decimal d = activity.Bar.Get(?)
string s = activity.Baz.Get(?)

The T Get() method on OutArgument<T> that requires an ActivityContext which I'm not sure how to obtain in code. OutArgument<T>上的T Get()方法需要一个ActivityContext ,我不确定该如何从代码中获取。

I also realize it's possible to get the un-typed values from result["Bar"] and result["Baz"] and cast them, but I'm hoping there's another way. 我也意识到可以从result["Bar"]result["Baz"]获取未类型化的值并将其result["Baz"] ,但是我希望有另一种方法。

Updated to make it clear there are multiple Out values, although the question would still apply even if there was only one. 已更新,以明确存在多个Out值,尽管即使只有一个,该问题仍然适用。

If you look at workflows as code, an Activity is no more than a method that receives input arguments and (potentially) returns output arguments. 如果将工作流视为代码,那么“活动”只不过是一种接收输入参数并(可能)返回输出参数的方法。

It happens that Activities allows one to return multiple output arguments, something that C# methods, for example, don't ( actually that's about to change with C# 7 and tuples ). 碰巧,Activity允许一个人返回多个输出参数,例如C#方法不这样做( 实际上这将随着C#7和元组而改变 )。

That's why you've an WorkflowInvoker.Invoke() overload which returns a Dictionary<string, object> because the framework obviously doesn't know what\\how many\\of what type output arguments you have. 这就是为什么您有一个WorkflowInvoker.Invoke()重载,该重载返回Dictionary<string, object>原因Dictionary<string, object>因为该框架显然不知道您拥有多少类型的输出参数。

Bottom line, the only way for you to do it fully strong-typed is exactly the same way you would be doing on a normal C# method - return one OutArgument of a custom type : 最重要的是,唯一实现完全强类型化的唯一方法与使用普通C#方法的方式完全相同- 返回一个自定义类型的OutArgument

public class ActivityFooOutput
{
    public decimal Bar { get; set }
    public decimal Baz { get; set; }
}

// generated class

public partial class ActivityFoo : System.Activities.Activity....
{
    public System.Activities.OutArgument<ActivityFooOutput> Result { ... }
}

// everything's strongly-typed from here on

var result = WorkflowInvoker.Invoke<ActivityFooOutput>(activity);

decimal d = result.Bar;
string s result.Baz;

Actually, if you don't want to create a custom type for it, you can use said tuples: 实际上,如果您不想为其创建自定义类型,则可以使用上述元组:

// generated

public System.Activities.OutArgument<Tuple<decimal, string>> Result { ... }

// everything's strongly-typed from here on

var result = WorkflowInvoker.Invoke<Tuple<decimal, string>>(activity);

decimal d = result.Item1;
string s result.Item2;

Being the first option obviously more scalable and verbose. 作为第一个选择,显然更具可扩展性和冗长性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM