成都创新互联网站制作重庆分公司

WF4.0Beta2中的Switch<T>是什么

这篇文章将为大家详细讲解有关WF4.0 Beta2中的Switch是什么,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联是一家专注于网站建设、网站制作与策划设计,裕华网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:裕华等地区。裕华做网站价格咨询:18982081108

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

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

1.下面是Person类,在Person类中我们必须要重写Equals方法和GetHashCode方法,代码如下:

[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.TypeConverter 类是.NET提供的类型换器 就是将一种类型(object,可以说是任何类型)转换到另一种类型(一般为string),或者将另一种类型转换回来。
我们实现上面的Person的PersonConverter,如下:

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 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);          }      }

3.工作流设计如下:

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

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

WF4.0 Beta2中的Switch<T>是什么

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

关于“WF4.0 Beta2中的Switch是什么”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


本文名称:WF4.0Beta2中的Switch<T>是什么
URL链接:http://cxhlcq.com/article/gggsij.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部