溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-12-27 09:42:36 來(lái)源:億速云 閱讀:150 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)WF4.0 Beta2中的Switch<T>是什么,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

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

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

1.下面是Person類,在Person類中我們必須要重寫(xiě)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,可以說(shuō)是任何類型)轉(zhuǎn)換到另一種類型(一般為string),或者將另一種類型轉(zhuǎn)換回來(lái)。
我們實(shí)現(xiàn)上面的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.工作流設(shè)計(jì)如下:

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

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

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

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

關(guān)于“WF4.0 Beta2中的Switch<T>是什么”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI