从 WF 4 活动动态设置外部范围中的变量

发布于 2024-10-05 13:08:53 字数 1278 浏览 1 评论 0原文

如何在 .NET 4 下的 Windows Workflow Foundation 活动中动态设置父范围中的变量值?

失败的尝试(放置在工作流上的序列活动,其中序列具有名为 Test 的 int 变量):

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”)直到运行时才知道。

How can I dynamically set the values of variables from the parent scope in a Windows Workflow Foundation activity under .NET 4?

An attempt that failed (drop on a Sequence activity on a workflow where the Sequence has an int variable named Test):

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

This results in:

System.InvalidOperationException was
unhandled by user code
Message=Activity '1.2: CodeActivity1'
cannot access this variable because it
is declared at the scope of activity
'1.1: Sequence'. An activity can only
access its own implementation
variables.

It does find the variable; it just can't get or set its value.

The variable name ("Test" in the above example) would not be known until runtime.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2024-10-12 13:08:53

处理此问题的正常方法是定义 OutArgument 并在工作流设计器中将 OutArgument 绑定到您的变量。在活动中,您只处理论证。使用 NativeActivity 会为您提供一个名为 Result 的 OutArgument,但只需添加 OUTArgument 的属性就可以了。

另一个好处是您不需要知道存储结果的“神奇”变量名称。

更新,因为下面注释中的代码不可读。

尝试在其爆炸的行之前添加以下内容:

var pi = context.GetType().GetProperty("AllowChainedEnvironmentAccess", BindingFlags.NonPublic | BindingFlags.Instance); 
pi.SetValue(context, true, null); 

完全不支持,所以请小心使用:-)

The normal way of handling this is to define an OutArgument and in the workflow designer bind the OutArgument to your variable. In the activity you only work with the argument. Using a NativeActivity gives you an OutArgument named Result but just adding a property of OUtArgument will do just fine.

Another benefit is you don't need to know "magic" variable names to store results in.

Update because the code in the comment below is unreadable.

Try adding the following just before the line it blows up:

var pi = context.GetType().GetProperty("AllowChainedEnvironmentAccess", BindingFlags.NonPublic | BindingFlags.Instance); 
pi.SetValue(context, true, null); 

Totally not supported so use with care :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文