详解WF4.0 Beta2中的Switch<T>活动

开发 后端
在这里我们将介绍WF4.0 Beta2中新增的Switch<T>活动,希望本文能让大家对WF4.0 Beta2的一些新特性有所了解。

对于微软的WF工作流,很多开发人员都有过接触。对于新版的WF4.0 Beta2,有许多新特性值得我们去开发和体验。这些新特性能给我们带来事半功倍的效果。

Switch<T>是WF4.0中新增的活动。功能类似于C#语言中的Switch语句,但是C#的Switch语句只能是一般的Int,String等类型。在WF4.0中Switch<T>可以使用
用于自定义的复杂类型。下面例子完成根据不同的Person执行不同的分支。

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,可以说是任何类型)转换到另一种类型(一般为string),或者将另一种类型转换回来。
我们实现上面的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.工作流设计如下:

3.1.定义一个Person类型的变量p1,Scope为Sequence。

3.2.工作流设计中首先是一个Assign活动来实例化p1,然后在Switc<Person>中根据p1的不同值来判断走不同的分支。

实例化截图

3.3.运行程序结果为:Hello Cary。

原文标题:WF4.0 Beta2:Switch活动中使用复杂类型

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

【编辑推荐】

  1. 浅谈WF 4.0 Beta1中的 跟踪机制
  2. WF4.0 Beta1中的规则引擎变化
  3. 浅谈WF 4.0 beta1的跟踪配置
  4. 详解工作流架构与实现
  5. 解析UML工作流管理系统
责任编辑:彭凡 来源: 博客园
相关推荐

2009-06-17 10:51:58

WF4.0规则引擎

2009-10-26 09:16:08

BigInteger类

2010-01-14 09:35:10

WF4.0

2009-10-30 09:04:18

WF4 Beta2

2009-07-16 10:41:40

WF 4.0 beta

2009-08-27 13:56:03

IEnumerable

2009-06-15 10:20:47

WF 4.0 Beta跟踪机制

2009-10-22 08:54:56

WF4 Beta 2

2010-01-20 09:34:26

List<T>

2009-06-22 09:36:06

WF 4.0 beta跟踪配置

2010-01-14 14:12:14

Visual Stud

2009-11-25 11:31:36

Visual Stud

2010-02-01 09:19:32

WF 4.0

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

2021-08-13 10:09:36

鸿蒙HarmonyOS应用

2013-06-25 10:13:11

iOS7WWDC苹果

2009-12-14 09:40:50

VS 2008 Bet

2011-09-27 10:40:48

Ubuntu 11.1
点赞
收藏

51CTO技术栈公众号