Понял! Глядя на блог Мориса, я вдохновился на это
Рабочий процесс (очень, очень просто!)
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();
}
выход
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!
Осуществление
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; }
}
}
Если кто-нибудь знает, как сделать это лучше, пожалуйста, не стесняйтесь, сообщите мне. Мне также нужно будет передать значения вложенным действиям