Jan 5

delegate初探 不指定

kcao , 15:12 , 技术 , 评论(0) , 引用(0) , 阅读(1358) , Via 本站原创 | |

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的步骤:

  1. delegate在使用前一定要先定义好,而且此定义必须要让类A和类B都能可见
  2. delegate定义时的参数要和目标函数的参数保持一致
  3. 需要的话,在调用类A中可以把此delegate对象当作参数,以备回调用
  4. 在要调用目标函数前,必须先声明一个delegate实例,即把目标函数填入此delegate对象中,基本语法:vvCallBack newDeleInstance = new vvCallBack(TargetFunctionNameWithoutParameter);
  5. 把此delegate实例当作参数,被类A的函数调用;此delegate也可以直接执行,如文末的例子一样,就用 newdeleInstance.Invoke();

 

C# 代码复制内容到剪贴板
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4. using System.Collections;    
  5.      
  6. namespace DelegateStudy1    
  7. {    
  8. /**//*This is declaration of new data type "CallBack" which is based   
  9. * on C# data type "delegate"*/    
  10. public delegate void vvCallBack(string s);    
  11.      
  12. class    
  13. {    
  14. private string m_ObjectName;    
  15. private string m_Color;    
  16.      
  17. public DataItem(string Name, string Color)    
  18. {    
  19. m_ObjectName = Name;    
  20. m_Color = Color;    
  21. }    
  22.      
  23. public string Name    
  24. {get {return m_ObjectName;}}    
  25.      
  26. public string Color    
  27. {get {return m_Color;}}    
  28.      
  29. }//Class Item    
  30.      
  31.      
  32. /**//*This class represents collection of data items.*/    
  33. class DataHolder    
  34. {    
  35. private ArrayList m_List;    
  36.      
  37. public DataHolder()    
  38. {    
  39. m_List = new ArrayList();    
  40. m_List.Add(new DataItem("Desk""Black"));    
  41. m_List.Add(new DataItem("Car""Red"));    
  42. m_List.Add(new DataItem("Train""Yellow"));    
  43. m_List.Add(new DataItem("Tomato""Red"));    
  44. m_List.Add(new DataItem("Cheese""Yellow"));    
  45. m_List.Add(new DataItem("Computer","Grey"));    
  46. m_List.Add(new DataItem("Sausage""Red"));    
  47. m_List.Add(new DataItem("Cat""Black"));    
  48. }    
  49.      
  50. /**//* this method provides search functionality for caller. When an item   
  51. * meets search condition is found, client defined procedure activates.*/    
  52. public void SearchData(string SearchCondition, vvCallBack ClientDefinedProcedure)    
  53. {    
  54. foreach (DataItem i in this.m_List)    
  55. {    
  56. if (i.Color == SearchCondition)    
  57. {    
  58. ClientDefinedProcedure(i.Name);    
  59. }    
  60. }    
  61. }    
  62. }//Class DataHolder    
  63.      
  64.      
  65. class MainClass    
  66. {    
  67. [STAThread]    
  68. static void Main(string[] args)    
  69. {    
  70. /**//*Create list of Data Items*/    
  71. DataHolder lst = new DataHolder();    
  72.      
  73. /**//* Cteate instance of delegate with type CallBack and bind it   
  74. * to user defined procedure PrintSearchResults()*/    
  75. vvCallBack procedure01 = new vvCallBack(PrintSearchResult);    
  76.      
  77. Console.WriteLine("Red color objects:");    
  78. /**//*Call search method with search critheria "Red" and user defined   
  79. * procedure which is nesessary to execute when data item meets to search   
  80. * creteria is found.*/    
  81. lst.SearchData("Red", procedure01);    
  82. Console.WriteLine("---------------");    
  83. Console.ReadLine();    
  84.      
  85. }    
  86.      
  87. /**//*This procedure is called by DataHolder any time when data item meets to search   
  88. * criteria is found.*/    
  89. private static void PrintSearchResult(string s)    
  90. {    
  91. Console.WriteLine(s);    
  92. }    
  93. }//MainClass    
  94.      
  95. }    

 

C# 代码复制内容到剪贴板
  1. public class Class1    
  2. {    
  3. public delegate int myFunction(int min);    
  4. public int MyOtherFunction(int mer)    
  5. {    
  6. int mal = 4;    
  7. return mer * mal;    
  8. }    
  9.      
  10. public static void Main()    
  11. {    
  12. Class1 mb = new Class1();    
  13. myFunction myD = new myFunction(mb.MyOtherFunction);    
  14. int returnValue = myD.Invoke(8);    
  15. console.WriteLine(returnValue);    
  16. Console.Read();    
  17. }    
  18. }    

 

Tags: , , ,
发表评论
Please log on to continue. Thanks!
打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]