Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3457448
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:51:40+00:00 2026-05-18T09:51:40+00:00

How can I dynamically set the values of variables from the parent scope in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:51:41+00:00Added an answer on May 18, 2026 at 9:51 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to dynamically set css with variables I retrieve from what a
Using jQuery , how can I dynamically set the size attribute of a select
With ASP.NET MVC (or using HttpHandlers) you can dynamically generate URLs, like the one
How can I dynamically load a usercontrol in ASP.NET if I dont have access
I want to be access some variables I've assigned dynamically from PHP in Smarty,
I want to know I can dynamically modify an existing Crystal Report (using C#
Trying to make a make generic select control that I can dynamically add elements
I have a custom class loader so that a desktop application can dynamically start
In Java, I can dynamically add stuff to classpath and load classes (dynamically meaning
Can I dynamically create an XAML and pop it into my app? How would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.