自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解WF4.0 Beta2中的Switch<T>活動

開發(fā) 后端
在這里我們將介紹WF4.0 Beta2中新增的Switch<T>活動,希望本文能讓大家對WF4.0 Beta2的一些新特性有所了解。

對于微軟的WF工作流,很多開發(fā)人員都有過接觸。對于新版的WF4.0 Beta2,有許多新特性值得我們?nèi)ラ_發(fā)和體驗(yàn)。這些新特性能給我們帶來事半功倍的效果。

Switch<T>是WF4.0中新增的活動。功能類似于C#語言中的Switch語句,但是C#的Switch語句只能是一般的Int,String等類型。在WF4.0中Switch<T>可以使用
用于自定義的復(fù)雜類型。下面例子完成根據(jù)不同的Person執(zhí)行不同的分支。

1.下面是Person類,在Person類中我們必須要重寫Equals方法和GetHashCode方法,代碼如下:

  1. [TypeConverter(typeof(PersonConverter))]  
  2.     public class Person  
  3.     {  
  4.         public string Name { getset; }  
  5.         public int Age { getset; }  
  6.  
  7.         public Person()  
  8.         {  
  9.             this.Age = 15;  
  10.         }  
  11.  
  12.         public Person(string name, int age)  
  13.         {  
  14.             this.Name = name;  
  15.             this.Age = age;  
  16.         }  
  17.  
  18.         public Person(string name) : this()  
  19.         {  
  20.             this.Name = name;  
  21.         }  
  22.  
  23.         public override bool Equals(object obj)  
  24.         {  
  25.             Person person = obj as Person;  
  26.             if (person != null)  
  27.             {  
  28.                 return string.Equals(this.Name, person.Name);  
  29.             }  
  30.             return false;  
  31.         }  
  32.  
  33.         public override int GetHashCode()  
  34.         {  
  35.             if (this.Name != null)  
  36.             {  
  37.                 return this.Name.GetHashCode();  
  38.             }  
  39.             return 0;  
  40.         }  
  41.     } 

2.TypeConverter 類是.NET提供的類型換器 就是將一種類型(object,可以說是任何類型)轉(zhuǎn)換到另一種類型(一般為string),或者將另一種類型轉(zhuǎn)換回來。
我們實(shí)現(xiàn)上面的Person的PersonConverter,如下:

  1. public class PersonConverter : TypeConverter  
  2.     {  
  3.         public override bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType)  
  4.         {  
  5.             return (sourceType == typeof(string));  
  6.         }  
  7.           
  8.         public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value)  
  9.         {  
  10.             if (value == null)  
  11.             {  
  12.                 return null;  
  13.             }  
  14.             if (value is string)  
  15.             {  
  16.                 return new Person  
  17.                 {  
  18.                     Name = (string)value  
  19.                 };  
  20.             }  
  21.             return base.ConvertFrom(context, culture, value);  
  22.         }  
  23.           
  24.         public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,                                          object value, Type destinationType)  
  25.         {  
  26.             if (destinationType == typeof(string))  
  27.             {  
  28.                 if (value != null)  
  29.                 {  
  30.                     return ((Person)value).Name;  
  31.                 }  
  32.                 else 
  33.                 {  
  34.                     return null;  
  35.                 }  
  36.             }  
  37.             return base.ConvertTo(context, culture, value, destinationType);  
  38.         }  
  39.     } 

3.工作流設(shè)計(jì)如下:

3.1.定義一個Person類型的變量p1,Scope為Sequence。

3.2.工作流設(shè)計(jì)中首先是一個Assign活動來實(shí)例化p1,然后在Switc<Person>中根據(jù)p1的不同值來判斷走不同的分支。

實(shí)例化截圖

3.3.運(yùn)行程序結(jié)果為:Hello Cary。

原文標(biāo)題:WF4.0 Beta2:Switch活動中使用復(fù)雜類型

鏈接:http://www.cnblogs.com/carysun/archive/2009/10/27/wf4-beta2-switch.html

【編輯推薦】

  1. 淺談WF 4.0 Beta1中的 跟蹤機(jī)制
  2. WF4.0 Beta1中的規(guī)則引擎變化
  3. 淺談WF 4.0 beta1的跟蹤配置
  4. 詳解工作流架構(gòu)與實(shí)現(xiàn)
  5. 解析UML工作流管理系統(tǒng)
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2009-06-17 10:51:58

WF4.0規(guī)則引擎

2009-10-26 09:16:08

BigInteger類

2010-01-14 09:35:10

WF4.0

2009-07-16 10:41:40

WF 4.0 beta

2009-10-30 09:04:18

WF4 Beta2

2009-08-27 13:56:03

IEnumerable

2009-06-15 10:20:47

WF 4.0 Beta跟蹤機(jī)制

2009-10-22 08:54:56

WF4 Beta 2

2010-01-14 14:12:14

Visual Stud

2009-06-22 09:36:06

WF 4.0 beta跟蹤配置

2010-02-01 09:19:32

WF 4.0

2010-01-20 09:34:26

List<T>

2009-11-25 11:31:36

Visual Stud

2013-10-10 15:08:36

UbuntuUbuntu 13.1gnome

2023-08-07 15:42:25

ArkUI-X鴻蒙

2012-06-12 13:23:58

LinuxLinux Deepi

2009-07-28 10:00:47

VS2010 beta

2021-08-13 10:09:36

鴻蒙HarmonyOS應(yīng)用

2013-06-25 10:13:11

iOS7WWDC蘋果

2009-12-14 09:40:50

VS 2008 Bet
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號