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

WF4.0 Beta2:InvokeMethod Activity的使用

Posted on 2009-11-06 22:08  生鱼片  阅读(1687)  评论(3编辑  收藏  举报

1.       用来调用指定类或对象的公有方法。

2.       几个比较重要的属性:

GenericTypeArguments:泛型参数集合

MethodName:调用的方法名称

Parameters:方法参数集合

Result:方法返回值

RunAsynchronously:是否以异步方式运行

TargetObject:调用方法的对象

TargetType:调用方法的类型

 

下面举几个典型的例子:

1.有参数有返回值

public class TestClass

    {

        public int TestdWithResult(int param1, int param2)

        {

            Console.WriteLine("Test With Result Value");

            Console.WriteLine(string.Format("param1 (int): {0}", param1));

            Console.WriteLine(string.Format("param2 (int): {0}", param2));

            return param1 + param2;

        }

    }

clip_image002

 

2.泛型静态方法

public static void GenericStaticMethod<T1, T2>(T1 param1, T2 param2)

        {

            Console.WriteLine("Generic Static Method with Parameters");

            Console.WriteLine(string.Format("typeof T1: {0}", typeof(T1)));

            Console.WriteLine(string.Format("param1: {0}", param1));

            Console.WriteLine(string.Format("typeof T2: {0}", typeof(T2)));

            Console.WriteLine(string.Format("param2: {0}", param2));

        }

2.1. 静态方法指定TargetType属性,实例方法指定TargetObject属性

2.2.指定GenericTypeArguments属性

clip_image004

 

3.       Ref参数的方法

public void RefMethod(string param1, int param2, ref string param3)

        {

            Console.WriteLine("Ref Method with Parameters");

            Console.WriteLine(string.Format("param1 (string): {0}", param1));

            Console.WriteLine(string.Format("param2 (int): {0}", param2));

            Console.WriteLine(string.Format("out param3 (string): {0}", param3));

            param3 = "reference param changed!";

        }

参数为in/out,绑定到变量:

clip_image006

4.       异步方法

// to be used for the async sample

        AsyncCallback callback;

        IAsyncResult asyncResult;       

        public void AsyncMethodSample(string message)

        {

            Console.WriteLine("....Called synchronous sample method with \"{0}\"", message);

            Console.WriteLine("....This method will not be executed, BeginAsyncMethoSample / EndAsyncMethodSample will be executed instead");

        } 

        // begin the async operation

        public IAsyncResult BeginAsyncMethodSample(string message, AsyncCallback callback, object asyncState)

        {

            Console.WriteLine("....BeginAsyncMethodSample");

            Console.WriteLine(string.Format("........Message: {0}", message));

            this.callback = callback;

            this.asyncResult = new TestAsyncResult() { AsyncState = asyncState }; 

            Thread t = new Thread(new ThreadStart(ProcessThread));

            t.Start(); 

            return this.asyncResult;

        } 

        // do some stuff and invoke the async callback

        public void ProcessThread()

        {

            Console.Write("........Processing ");

            for (int i = 0; i < 5; i++)

            {

                Console.Write(". ");

                Thread.Sleep(500);

            }

            Console.WriteLine(); 

            this.callback(this.asyncResult);

        }

         // end the async operation

        public void EndAsyncMethodSample(IAsyncResult r)
        {

            Console.WriteLine("....EndAsyncMethodSample");

        } 

        class TestAsyncResult : IAsyncResult

        {

            public object AsyncState

            {

                get;

                set;

            } 

            public WaitHandle AsyncWaitHandle

            {

                get { throw new NotImplementedException(); }

            } 

            public bool CompletedSynchronously

            {

                get { return true; }

            } 

            public bool IsCompleted

            {

                get { return true; }

            }

        }

4.1.和其他不同的是我们要勾选RunAsynchronously属性

4.2.MethodName属性的 AsyncMethodSample方法不会调用,调用的是beginend的方法。