How to Determine the Arguments of an Arbitrary Activity (WF)

How to Determine the Arguments of an Arbitrary Activity (WF)

Your application may need to interact with an arbitrary activity at design-time or run-time, such as when an activity is dropped into a rehosted workflow designer. In this case, you may want to determine the arguments that the activity is expecting. Since an activity's arguments are exposed as properties, you can use reflection to determine this. If the activity is of type DynamicActivity (such as would be loaded from serialization), the arguments are also exposed in the Properties collection.

The following code demonstrates how to determine the name of each argument.

using System.Reflection;

....

Type activityType = typeof(YourActivity);
var props = activityType.GetProperties();
foreach (var prop in props)
{
    if (prop.PropertyType.ToString().StartsWith("System.Activities.InArgument"))
        Console.WriteLine("Property found: {0}, Type: {1}", prop.Name, prop.PropertyType.ToString());
}

If you need to determine the generic type of the arguments, you can use a PropertyDescriptor to enumerate the types:

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(wf);
foreach (PropertyDescriptor property in properties)
{
    if (property.PropertyType.IsGenericType && 
        (property.PropertyType.GetGenericTypeDefinition() == typeof(InArgument<>) ||
        property.PropertyType.GetGenericTypeDefinition() == typeof(InOutArgument<>)))
    {
        Type targetType = property.PropertyType.GetGenericArguments()[0];
        Console.WriteLine("Name: {0}, Type: {1}", property.Name, targetType);
    }
}

For the following workflow which has two in arguments:

InArgument<int> Operand1 = new InArgument<int>();
InArgument<int> Operand2 = new InArgument<int>();

DynamicActivity<int> wf = new DynamicActivity<int>
{
    Properties =
    {
        new DynamicActivityProperty
        {
            Name = "Operand1",
            Type = typeof(InArgument<int>),
            Value = Operand1
        },
        new DynamicActivityProperty
        {
            Name = "Operand2",
            Type = typeof(InArgument<int>),
            Value = Operand2
        }
    },

    Implementation = () => new Sequence
    {
        Activities = 
        {
            new Assign<int>
            {
                To = new ArgumentReference<int> { ArgumentName = "Result" },
                Value = new InArgument<int>((env) => Operand1.Get(env) + Operand2.Get(env))
            }
        }
    }
};

The code above would give the following output:

Name: Operand1, Type: System.Int32
Name: Operand2, Type: System.Int32


Leave a Comment
  • Please add 8 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 4. Comment: Replace RGB values with color names in HTML to restore colors

  • Ed Price - MSFT edited Revision 1. Comment: Title casing and tags

Page 1 of 1 (2 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Ed Price - MSFT edited Revision 1. Comment: Title casing and tags

  • Richard Mueller edited Revision 4. Comment: Replace RGB values with color names in HTML to restore colors

Page 1 of 1 (2 items)