Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Switch in WF4.0 Beta2

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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

对于微软的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的不同值来判断走不同的分支。

3.3. The result of running the program is: Hello Cary.

About "What is Switch in WF4.0 Beta2" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report