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 6561169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:33:14+00:00 2026-05-25T13:33:14+00:00

public sealed class Parent : NativeActivity { public Parent() { Childrens = new Collection<Activity>();

  • 0
public sealed class Parent : NativeActivity
{
    public Parent()
    {
        Childrens = new Collection<Activity>();
        Variables = new Collection<Variable>();

        _currentActivityIndex = new Variable<int>();
        CurrentCustomTypeInstance= new Variable<MyCustomType>();
    }

    [Browsable(false)]
    public Collection<Activity> Childrens { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
        _currentActivityIndex.Set(context, 0);
        context.ScheduleActivity(FirstActivity, Callback);
    }

    private void Callback(NativeActivityContext context, ActivityInstance completedInstance, MyCustomType customTypeInstance)
    {
        CurrentCustomTypeInstance.Set(context, customTypeInstance);
        ScheduleNextChildren(context, completedInstance);
    }

    private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
    {
        int nextActivityIndex = _currentActivityIndex.Get(context) + 1;
        if (nextActivityIndex >= Childrens.Count)
            return;

        Activity nextActivity = Childrens[nextActivityIndex];

        IFoo nextActivityAsIFoo = nextActivity as IFoo;
        if (nextActivityAsIFoo != null)
        {
            var currentCustomTypeInstance = CurrentCustomTypeInstance.Get(context);
            // HERE IS MY EXCEPTION
            nextActivityAsIFoo.FooField.Set(context, currentCustomTypeInstance);
        }

        context.ScheduleActivity(nextActivity);
        _currentActivityIndex.Set(context, nextActivityIndex);

    }
}

And in register metadata:

metadata.SetChildrenCollection(Childrens);

I’ve already read http://msmvps.com/blogs/theproblemsolver/archive/2011/04/05/scheduling-child-activities-with-input-parameters.aspx but in my case, parent does not know the child activity

Edit

Similar to: Activity cannot set a Variable defined within its scope?

Activity '1.1: Parent' cannot access this variable because it is declared at the scope
of activity '1.1: Parent'. An activity can only access its own implementation variables.

But in my case, I don’t need to get the return value, so, hope to be easier. Just need to pass FooField implicitly instead of leting it to flow author.
I need to do it implicitly! If it doesn’t work at all, I will go with NativeActivityContext Properties

  • 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-25T13:33:15+00:00Added an answer on May 25, 2026 at 1:33 pm

    Got it! Looking at the Maurice’s blog I got inspired to do it

    Workflow(very very simple!)

    static void Main(string[] args)
    {
        Parent parent = new Parent();
        parent.Childrens.Add(new FooWriter());
        parent.Childrens.Add(new FooFormater());
        parent.Childrens.Add(new FooWriter());
    
        WorkflowInvoker.Invoke(parent);
        Console.Read();
    }
    

    Output

    What's the Foo name?
    Implicit FTW!
    Im a custom Foo Handler, my Foo name is: Implicit FTW!
    Im a custom Foo Handler, my Foo name is: IMPLICIT FTW!
    

    Implementation

    using System;
    using System.Activities;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    
    namespace WorkflowConsoleApplication2
    {
    // Parent class that creates a Foo and passes it to their childrens
    public sealed class Parent : NativeActivity
    {
        private Variable<int> _currentActivityIndex;
        private Variable<Foo> _currentFoo;
    
        public Parent()
        {
            Childrens = new Collection<Activity>();
            _executionChildrens = new Collection<Tuple<Activity, ActivityAction<Foo>>>();
    
            _currentActivityIndex = new Variable<int>();
            _currentFoo = new Variable<Foo>();
        }
    
        [Browsable(false)]
        public Collection<Activity> Childrens { get; set; }
        private Collection<Tuple<Activity, ActivityAction<Foo>>> _executionChildrens;
    
        protected override void Execute(NativeActivityContext context)
        {
            Console.WriteLine("What's the Foo name?");
            _currentFoo.Set(context, new Foo { Name = Console.ReadLine() });
    
            _currentActivityIndex.Set(context, 0);
            ScheduleNextChildren(context, null);
        }
    
        private void ScheduleNextChildren(NativeActivityContext context, ActivityInstance completedInstance)
        {
            int currentActivityIndex = _currentActivityIndex.Get(context);
            if (currentActivityIndex >= Childrens.Count)
                return;
    
            Tuple<Activity, ActivityAction<Foo>> nextActivity = _executionChildrens[currentActivityIndex];
    
            if (IsFooHandler(nextActivity))
            {
                context.ScheduleAction(nextActivity.Item2, _currentFoo.Get(context),
                    ScheduleNextChildren);
            }
            else
            {
                context.ScheduleActivity(nextActivity.Item1,
                    ScheduleNextChildren);
            }
    
            _currentActivityIndex.Set(context, currentActivityIndex + 1);
    
        }
    
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.SetArgumentsCollection(metadata.GetArgumentsWithReflection());
    
            metadata.AddImplementationVariable(_currentActivityIndex);
            metadata.AddImplementationVariable(_currentFoo);
    
            RegisterChildrens(metadata, Childrens);
            // remove "base.Cachemetadata" to "Childrens collection" doesn't become a child   again        }
    
        public void RegisterChildrens(NativeActivityMetadata metadata, IEnumerable<Activity> childrens)
        {
            foreach (Activity child in childrens)
            {
                IFooHandler childAsIFooHandler = child as IFooHandler;
                if (childAsIFooHandler != null)
                {
                    ActivityAction<Foo> childsWrapperAction = new ActivityAction<Foo>();
    
                    var activityToActionBinderArgument = new DelegateInArgument<Foo>();
                    childsWrapperAction.Argument =  activityToActionBinderArgument;
                    childAsIFooHandler.Foo =        activityToActionBinderArgument;
    
                    childsWrapperAction.Handler = child;
    
                    metadata.AddDelegate(childsWrapperAction);
                    _executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, childsWrapperAction));
                }
                else
                {
                    metadata.AddChild(child);
                    _executionChildrens.Add(new Tuple<Activity, ActivityAction<Foo>>(child, null));
                }
            }
        }
    
        public static bool IsFooHandler(Tuple<Activity, ActivityAction<Foo>> activity)
        {
            return activity.Item2 != null;
        }
    }
    
    // samples of Foo handlers
    public class FooWriter : CodeActivity, IFooHandler
    {
        /// When FooWriter is direct child of "Parent" this argument is passed implicitly
        public InArgument<Foo> Foo { get; set; }
    
        protected override void Execute(CodeActivityContext context)
        {
            Console.WriteLine("Im a custom Foo Handler, my Foo name is: {0}", Foo.Get(context).Name);
        }
    }
    
    public class FooFormater : CodeActivity, IFooHandler
    {
        public InArgument<Foo> Foo { get; set; }
    
        protected override void Execute(CodeActivityContext context)
        {
            Foo foo = Foo.Get(context);
            foo.Name = foo.Name.ToUpper();
        }
    }
    
    // sample classes
    public class Foo
    {
        public string Name { get; set; }
    }
    
    public interface IFooHandler
    {
        InArgument<Foo> Foo { get; set; }
    }
    }
    

    If anyone know how to do it in a better way please fell free to tell me. I will also need to pass the values to nested activities

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

Sidebar

Related Questions

public sealed class SurrogateSelector : System.Runtime.Serialization.SurrogateSelector, System.Runtime.Serialization.ISurrogateSelector { System.Runtime.Serialization.ISerializationSurrogate ISS = System.Runtime.Serialization.FormatterServices.GetSurrogateForCyclicalReference(new SerializationSurrogate()); public
let's say i have a class: [Serializable] public sealed class MyFoo { public int
This is my setup: public class Parent { public virtual int Id { get;
c# public sealed class RC5 { private readonly uint[] _bufKey = new uint[4]; private
I have created a custom activity with an InArgument like so: public sealed class
using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; public sealed class Foo { public int ID;
Given a class: [DataContract] public sealed class ChangedField { [DataMember(Name=I)] public ushort FieldId {
My program has the following class definition: public sealed class Subscriber { private subscription;
This causes a compile-time exception: public sealed class ValidatesAttribute<T> : Attribute { } [Validates<string>]
I implemented a Singleton pattern like this: public sealed class MyClass { ... public

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.