gohenderson
gohenderson

Reputation: 25

Query Current State for Possible Future States in State Machine

I'm looking for a way to query a state in a state machine to find out what the possible destinations are for that state. I know that one option is to parse the Xaml for the information that I need. Is there another way? Possibly by querying the state object in .NET code?

The goal is to decrease the number of places that the business logic for the state transitions is stored.

Upvotes: 2

Views: 749

Answers (2)

Dave Clausen
Dave Clausen

Reputation: 1320

I faced this question too today - I thought mperrenoud03's code would solve it, but alas it didn't work for me under .NET 4.5 RC. The reason is that it reflected an internal/private type System.Activities.Statements.InternalState rather than System.Activities.Statements.State.

The InternalState object had a Transitions collection, which I could probably obtain by further reflection ... but I felt this took me way past the smell test.

A solution I found which is working so far is this NuGet: http://nuget.org/packages/Microsoft.Activities.Extensions. It has a WF Extension called StateMachineStateTracker. At any time, you can ask it for the current StateMachine, and its collection of Transitions.

Upvotes: 3

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Yes, you can. If you issue the following statement you can get the state machine itself from a custom activity.

this.GetType().GetProperty("Parent",
    System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance).GetValue(this, null)

Then on the state machine object is a property named Transitions -- each Transition has a To property incidating what State they transition to.

Incidentally you can also grab the Trigger and Condition from that object! :)

I hope this helps!

Upvotes: 2

Related Questions