一个简单的Delegate使用例子

来源:互联网 发布:阿里云cdn防盗链 编辑:程序博客网 时间:2024/05/09 02:43


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace delegateDemo
{
    class Program
    {
        static void OtherClassMethod()
        {
            Console.WriteLine("another class's static method");
        }

        static void Main(string[] args)
        {
            var test = new TestDelegate();
            test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod);
            test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod);
            test.delegateMethod += Program.OtherClassMethod;
            test.RunDelegateMethods();
        }
    }

    class TestDelegate
    {
        public delegate void DelegateMethod();  //声明了一个Delegate Type

        public DelegateMethod delegateMethod;   //声明了一个Delegate对象

        public static void StaticMethod()
        {
            Console.WriteLine("Delegate a static method");
        }

        public void NonStaticMethod()
        {
            Console.WriteLine("Delegate a non-static method");
        }

        public void RunDelegateMethods()
        {
            if (delegateMethod != null)
            {
                Console.WriteLine("---------");
                delegateMethod.Invoke();
                Console.WriteLine("---------");
            }
        }
    }


}


0 0