博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

WF4集合Collection相关活动用法

Posted on 2011-01-20 14:47  生鱼片  阅读(2110)  评论(0编辑  收藏  举报

集合活动用于使用工作流中的集合对象。.NET Framework 版本 4包含多个系统提供的活动,用于在集合中添加和移除项、测试集合中是否存在某个项以及清除集合。所有集合活动都是继承自 CodeActivityCodeActivity 的泛型类;ExistsInCollectionRemoveFromCollection 具有一个类型为 BooleanOutArgument,用于指示结果。主要涉及以下相关活动:

clip_image002

 

1.写一个自定义活动用来显示集合中的内容

public sealed class PrintCollectionsActivity<T> : CodeActivity

    {

        // Define an activity input argument of type string

        public InArgument<ICollection<T>> InCollection { get; set; }

 

        // If your activity returns a value, derive from CodeActivity<TResult>

        // and return the value from the Execute method.

        protected override void Execute(CodeActivityContext context)

        {

            // Obtain the runtime value of the Text input argument

            ICollection<T> collection = InCollection.Get<ICollection<T>>(context);

            if (collection.Count > 0)

            {

                Console.WriteLine("---------------begin---------------");

                foreach (var item in collection)

                {

                    Console.WriteLine(item.ToString());

                }

                Console.WriteLine("----------------end----------------");

            }

            else

            {

                Console.WriteLine("Collection is empty!");

            }

        }

    }

2.在工作流中定义两个变量如下图:

clip_image004

 

3.设计工作流如下图:

clip_image006

4.在这个工作流中我们展示集合相关的四个活动的用法,详细的属性设置可以看上图,宿主代码如下:

class Program

    {

        static void Main(string[] args)

        {

            WorkflowInvoker.Invoke(new Workflow1());

            Console.ReadLine();

        }

    }

 

5.运行的结果如下:

clip_image008