Switch <t> is an activity added in wf4.0. The function is similar to the switch statement in C #, but the switch statement in C # can only be of the Int or string type. In wf4.0, switch <t> can be used
It is used to customize complex types. The following example shows how to execute different branches based on different persons.
1. The following is the person class. In the person class, we must rewrite the equals and gethashcode methods,CodeAs follows:
[ Typeconverter ( Typeof ( Personconverter )] Public class Person { Public String Name { Get ; Set ;} Public int Age { Get ; Set ;} Public Person (){ This . Age = 15 ;} Public Person ( String Name, Int Age ){ This . Name = Name; This . Age = age ;} Public Person ( String Name ):This (){ This . Name = Name ;} 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 ;}}
2.TypeconverterClass is. NET provides a type converter to convert one type (object, which can be said to be any type) to another type (generally string) or to another type.
We implement the personconverter of the person above, as shown below:
Public class Personconverter : Typeconverter { Public override bool Canconvertfrom ( Itypedescriptorcontext Context, Type Sourcetype ){ Return (Sourcetype = Typeof ( String ));} 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 );}Public override object Convertor ( Itypedescriptorcontext Context, Cultureinfo Culture,
Object Value, Type Destinationtype ){ If (Destinationtype = Typeof ( String )){ If (Value! = Null ){ Return ((Person ) Value). Name ;} Else { Return null ;}} Return Base . Convertize (context, culture, value, destinationtype );}}
3. The workflow is designed as follows:
3. 1. Define a person-type variable P1 and scope as sequence.
. In the workflow design, first an assign activity is used to instantiate P1, and then different branches are determined based on different P1 values in switc <person>.
3. RunProgramThe result is Hello Cary.