.Net Framework回调函数提供方式介绍

开发 后端
.Net Framework回调函数的操作在实际程序开发中是比较常用到的一个操作。作为刚刚上手的新手来说,就需要加强对这方面的了解。

.Net Framework回调函数可以通过委托的应用来实现。那么具体的操作步骤会在这篇文章中进行详细的解读,希望初学者们可以以此进行以此实际操作,以加强对这方面知识的认识程度,提高自己的代码编写效率。#t#

.Net Framework回调函数操作方法代码示例:

  1. class Set  
  2. {  
  3. private Object[] items;  
  4. public Set(int numItems)  
  5. {  
  6. items = new Object[numItems];  
  7. for (int i = 0; i < numItems; i++)  
  8. {  
  9. items[i] = i;  
  10. }  
  11. }  
  12. public delegate void Feedback
    (Object value, int item, int numItems);  
  13. //定义了一个共有委托类型Feedback,
    委托表示一个回调方法签名,故Feedback
    委托表示一个接受3个参数,且返回值为
    void的回调方法  
  14. public void ProcessItems
    (Feedback feedback)  
  15. {  
  16. for (int item = 0; item 
    < items.Length; item++)  
  17. {  
  18. if (feedback != null)  
  19. {  
  20. feedback(items[item],item+1,
    items.Length);  
  21. }  
  22. } //ProcessItems方法接受一个参数feedback,
    然后调用由feedback变量所指定的回调方法  
  23. }  

1.使用委托进行.Net Framework回调函数静态方法

 

  1. static void StaticCallbacks()  
  2. {  
  3. Set setofItems = new Set(5);  
  4. setofItems.ProcessItems(null);  
  5. //传递给feedback参数的值为null,
    不会调用任何回调方法  
  6. setofItems.ProcessItems(new
     Set.Feedback(/**//*method*/));  
  7. //构造一个委托对象,封装一个方法method,
    这使得该方法可以通过委托封装进行间接回调  
  8. Set.Feedback fb = null;  
  9. fb += new Set.Feedback(/**//*method1*/);  
  10. fb += new Set.Feedback(/**//*method2*/);  
  11. setofItems.ProcessItems(fb);  
  12. //委托链,所有回调的方法都必须接受
    相同参数,返回值类型  

2.使用委托进行.Net Framework回调函数实例方法

与调用静态方法不同的是,回调实例方法需要构造一个对象

 

  1. App appobj=new App();  
  2. setOfItems.ProcessItems
    (new Set.Feedback(appobj.
    FeedbackToFile)); 

 

责任编辑:曹凯 来源: 博客园
相关推荐

2009-08-12 10:11:18

C# 回调函数

2009-11-04 11:32:20

VB.NET回调函数

2009-12-15 11:28:34

.NET Framew

2009-07-20 16:12:21

ASP.NET Fra

2012-02-01 10:33:59

Java

2009-08-21 17:02:20

ASP.NET异步回调

2010-01-05 18:49:57

.NET Framew

2010-01-05 17:39:10

.NET Framew

2011-06-15 11:05:14

C语言回调函数

2011-07-25 14:32:40

Cocoa 框架 函数

2019-11-05 10:03:08

callback回调函数javascript

2022-04-12 08:30:52

回调函数代码调试

2011-05-20 17:19:25

回调函数

2010-01-05 18:21:33

.NET Framew

2009-08-19 17:10:09

C#回调函数

2011-05-20 17:59:06

回调函数

2010-02-04 16:07:39

C++回调函数

2009-12-08 15:52:10

WCF回调

2009-11-09 15:58:07

WCF回调方法

2010-01-06 10:07:35

.NET Framew
点赞
收藏

51CTO技术栈公众号