Jan
5
delegate(委托)是C#中一项令我比较费解的东西,在网上查到以下代码用以学习,根据MSDN中的说明,使用delegate可以让一个类(A)访问另一个类(B)的函数而不必是类(B)的成员.
在以下的代码中,在名称空间中定义了一个delegate对象vvCallBack(),它的参数就是准备要调用的目标函数(PrintSearchResult)的参数(string s). 这里注意,目标函数(PrintSearchResult)是一个private函数.
在类A中,通过第52行(public void SearchData(string SearchCondition, vvCallBack ClientDefinedProcedure))调用目标函数,这里无视目标函数是在其他哪个类中或者目标函数是不是public,只要在第58行中(ClientDefinedProcedure(i.Name);)用对参数即可
在类B中,第75行(vvCallBack procedure01 = new vvCallBack(PrintSearchResult);)把目标函数填入新建的delegate对象中,准备输入其他类中调用.
在第81行(lst.SearchData("Red", procedure01);)进行调用,由此,类B(MainClass)的PrintSearchResult函数通过delegate被类A(DataHolder)的函数调用了.
由此可见,在类B中,要使类A的成员返调类B的函数时,需要使用delegate.
总结一下使用delegate的步骤:
- delegate在使用前一定要先定义好,而且此定义必须要让类A和类B都能可见
- delegate定义时的参数要和目标函数的参数保持一致
- 需要的话,在调用类A中可以把此delegate对象当作参数,以备回调用
- 在要调用目标函数前,必须先声明一个delegate实例,即把目标函数填入此delegate对象中,基本语法:vvCallBack newDeleInstance = new vvCallBack(TargetFunctionNameWithoutParameter);
- 把此delegate实例当作参数,被类A的函数调用;此delegate也可以直接执行,如文末的例子一样,就用 newdeleInstance.Invoke();
C# 代码复制内容到剪贴板
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace DelegateStudy1
- {
- /**//*This is declaration of new data type "CallBack" which is based
- * on C# data type "delegate"*/
- public delegate void vvCallBack(string s);
- class
- {
- private string m_ObjectName;
- private string m_Color;
- public DataItem(string Name, string Color)
- {
- m_ObjectName = Name;
- m_Color = Color;
- }
- public string Name
- {get {return m_ObjectName;}}
- public string Color
- {get {return m_Color;}}
- }//Class Item
- /**//*This class represents collection of data items.*/
- class DataHolder
- {
- private ArrayList m_List;
- public DataHolder()
- {
- m_List = new ArrayList();
- m_List.Add(new DataItem("Desk", "Black"));
- m_List.Add(new DataItem("Car", "Red"));
- m_List.Add(new DataItem("Train", "Yellow"));
- m_List.Add(new DataItem("Tomato", "Red"));
- m_List.Add(new DataItem("Cheese", "Yellow"));
- m_List.Add(new DataItem("Computer","Grey"));
- m_List.Add(new DataItem("Sausage", "Red"));
- m_List.Add(new DataItem("Cat", "Black"));
- }
- /**//* this method provides search functionality for caller. When an item
- * meets search condition is found, client defined procedure activates.*/
- public void SearchData(string SearchCondition, vvCallBack ClientDefinedProcedure)
- {
- foreach (DataItem i in this.m_List)
- {
- if (i.Color == SearchCondition)
- {
- ClientDefinedProcedure(i.Name);
- }
- }
- }
- }//Class DataHolder
- class MainClass
- {
- [STAThread]
- static void Main(string[] args)
- {
- /**//*Create list of Data Items*/
- DataHolder lst = new DataHolder();
- /**//* Cteate instance of delegate with type CallBack and bind it
- * to user defined procedure PrintSearchResults()*/
- vvCallBack procedure01 = new vvCallBack(PrintSearchResult);
- Console.WriteLine("Red color objects:");
- /**//*Call search method with search critheria "Red" and user defined
- * procedure which is nesessary to execute when data item meets to search
- * creteria is found.*/
- lst.SearchData("Red", procedure01);
- Console.WriteLine("---------------");
- Console.ReadLine();
- }
- /**//*This procedure is called by DataHolder any time when data item meets to search
- * criteria is found.*/
- private static void PrintSearchResult(string s)
- {
- Console.WriteLine(s);
- }
- }//MainClass
- }
C# 代码复制内容到剪贴板
- public class Class1
- {
- public delegate int myFunction(int min);
- public int MyOtherFunction(int mer)
- {
- int mal = 4;
- return mer * mal;
- }
- public static void Main()
- {
- Class1 mb = new Class1();
- myFunction myD = new myFunction(mb.MyOtherFunction);
- int returnValue = myD.Invoke(8);
- console.WriteLine(returnValue);
- Console.Read();
- }
- }



