【发布时间】:2010-12-02 22:17:55
【问题描述】:
如何在 .NET 4 下的 Windows Workflow Foundation 活动中动态设置父范围的变量值?
失败的尝试(在 Sequence 具有名为 Test 的 int 变量的工作流中删除 Sequence 活动):
public sealed class CodeActivity1 : NativeActivity
{
protected override void CacheMetadata(NativeActivityMetadata metadata)
{
_locationReferences =
metadata.Environment.GetLocationReferences().ToList();
base.CacheMetadata(metadata);
}
protected override void Execute(NativeActivityContext context)
{
LocationReference locationReference =
_locationReferences.Find(
x => x.Name == "Test" && x.Type == typeof (int));
if (locationReference != null)
{
Console.WriteLine(
locationReference.Name + " " + locationReference.Type);
// Blows up here.
Location location = locationReference.GetLocation(context);
location.Value = 5;
}
}
private List<LocationReference> _locationReferences;
}
这会导致:
System.InvalidOperationException 是 用户代码未处理
消息=活动 '1.2: CodeActivity1' 无法访问此变量,因为它 在活动范围内声明 '1.1:序列'。一个活动只能 访问自己的实现 变量。
它确实找到了变量;它只是无法获取或设置其值。
变量名(上例中的“Test”)要到运行时才能知道。
【问题讨论】:
标签: workflow-foundation workflow-foundation-4