委托的认识

委托的用处:
委托是一个类型安全的对象,它指向程序中另一个以后会别调用的方法(或多个方法).


1委托包含3个重要的信息:

   (1)它所调用的方法的名称;

   (2)该方法的参数(可选)

   (3)该方法的返回值(可选)
2 .Net 委托可以指向静态方法,也可以指向实例方法
3 委托读被自动赋予同步或者异步方法方法的能力;


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

namespace SimpleDelegate
{
    class Program
    {
        //定义一个委托对象 返回一个整数,输入 2个整数x,y 委托的声明格式 决定所能调用方法的类型
        public delegate int OrderS(int x, int y);//
        //定义一个类  中有2个运算
        public class Yunsuan
        {
            public static int Add(int m, int n)
            {
                return m + n;
            }
            public static int Substract(int m, int n)
            {
                return m - n;
            }
        }

        static void Main(string[] args)
        {
            //创建一个指向Yunsuan.Add的委托对象
            OrderS s = new OrderS(Yunsuan.Add);
            //使用委托对象间接调用Add方法
            Console.WriteLine("10+10={0}", s(10, 10));
            Console.WriteLine();
            Console.ReadLine();

        }
    }
}

补充:
委托类型公有的核心成员:
Method: 此属性返回System.Refelction.MethodInfo对象,用以表示委托维护的静态方法的相信信息;
Target:  如果方法调用是定义在对象级别的(切记而不是静态方法),Target返回表示委托维护的方法的对象;如果Target返回值为null,调用的方法是一个静态成员;
Combine() 此静态方法给委托维护的列表添加一个方法.在C#中,使用重载+=操作符作为简化符号调用此方法;
GetInvocationList(): 此方法返回一个System.Delegate类型的数组,其中数组中的每个元素都表示可以调用的特定方法;
Remove() RemoveAll():这些静态方法从调用列表中移除一个(或所以)方法.在C#中,Remove(方法通过使用重载-=操作符来调用)

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


namespace SimpleDelegate
{
    class Program
    {
        //定义一个委托对象 返回一个整数,输入 2个整数x,y 委托的声明格式 决定所能调用方法的类型
        public delegate int OrderS(int x, int y);//
        //定义一个类  中有2个运算
        public class Yunsuan
        {
            public static int Add(int m, int n)
            {
                return m + n;
            }
            public int Substract(int m, int n)
            {
                return m - n;
            }

        }
        public static void DisplayDelegateInfo(Delegate a)//输入传入的委托的类型所维护的方法的名称和方法的类的名称
        {
            foreach(Delegate d in a.GetInvocationList())
            {//此属性返回System.Refelction.MethodInfo对象,用以表示委托维护的静态方法的相信信息;

                 Console.WriteLine("Method name:{0}",d.Method);
                 //如果方法调用是定义在对象级别的(切记而不是静态方法),
                //Target返回表示委托维护的方法的对象;如果Target返回值为null,调用的方法是一个静态

成员;
                 Console.WriteLine("Type name:{0}", d.Target);

            }
         }
       static void Main(string[] args)
        {
            //创建一个指向Yunsuan.Add的委托对象
            OrderS s = new OrderS(Yunsuan.Add);//add为类的静态方法 可以这样调用
           //Substract 不是类的静态方法,需要通过类的实例来调用
           Yunsuan Y=new Yunsuan();
           OrderS c = new OrderS(Y.Substract);//

            //使用委托对象间接调用Add方法
            Console.WriteLine("10+10={0}", s(10, 10));
            Console.WriteLine();
            DisplayDelegateInfo(s);//由于add方法为静态成员 所以返回的为空
            Console.WriteLine();
            DisplayDelegateInfo(c);
            Console.ReadLine();

        }
    }
}

posted @ 2013-03-06 16:06  啸月☆天狼  阅读(159)  评论(0编辑  收藏  举报