Export (0) Print
Expand All

Usage of the Switch Activity with Custom Types

This sample describes how to enable a Switch activity to evaluate a user-defined complex type at runtime. In most traditional procedural programming languages, a switch statement selects an execution logic based on the conditional evaluation of a variable. Traditionally, a switch statement operates on an expression that can be statically evaluated. For example, in C# this means that only primitive types, such as Boolean, Int32, String, and enumeration types are supported.

To enable switching on a custom class, logic must be implemented to evaluate values of the custom complex type at runtime. This sample demonstrates how to enable switching on a custom complex type named Person.

  • In the custom class Person, a TypeConverter attribute is declared with the name of the custom TypeConverter.

    [TypeConverter(typeof(PersonConverter))]
    public class Person
    {
       public string Name { get; set; }
       public int Age { get; set; }
    ...
    
    
  • In the custom class Person, the Equals and GetHashCode classes are overridden.

    public override bool Equals(object obj)
    {
        Person person = obj as Person;
    
        if (person != null)
        {
            return string.Equals(this.Name, person.Name);
        }
    
        return false;
    }
    
    public override int GetHashCode()
    {
        if (this.Name != null)
        {
            return this.Name.GetHashCode();
        }
    
        return 0;
    }
    
    
  • A custom TypeConverter class is implemented that performs the conversion of an instance of the custom class to a string and a string to an instance of a custom class.

    public class PersonConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context,
           Type sourceType)
        {
            return (sourceType is string);
        }
    
        // Overrides the ConvertFrom method of TypeConverter.
        public override object ConvertFrom(ITypeDescriptorContext context,
           CultureInfo culture, object value)
        {
            if (value == null)
            {
                return null;
            }
    
            if (value is string)
            {
                return new Person
                {
                    Name = (string)value
                };
            }
    
            return base.ConvertFrom(context, culture, value);
        }
    
        // Overrides the ConvertTo method of TypeConverter.
        public override object ConvertTo(ITypeDescriptorContext context,
           CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                if (value != null)
                {
                    return ((Person) value).Name;
                }
                else
                {
                    return null;
                }
            }
    
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
    

The following files are included in this sample:

  • Person.cs: Defines the Person class.

  • PersonConverter.cs: The type converter for the Person class.

  • Sequence.xaml: a workflow that switches over the Person type.

  • Program.cs: The main function that runs the workflow.

To use this sample

  1. Load Switch.sln in Visual Studio 2010.

  2. Press CTRL+SHIFT+B to build the solution.

  3. Press CTRL + F5 to run the sample.

Important Note:
The samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WF\Basic\Built-InActivities\Switch

See Also




Build Date:

2012-08-02
Show:
© 2014 Microsoft