Wf4.0 beta2: complex types used in switch Activity

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.