Delegate 的应用
Delegate是.NET的一个重要特性,并且非常有用。
也许知道delegate是怎么回事情但是如果没有用过它,就不会更深刻的认识它。好比看的懂某个知识点的代码,但是并不一定真的懂这个知识点。
对于此,我身有体会,我对delegate的了解就是这样一种情况。
还记得以前用C/C++写代码,CallBack感觉用起来不顺手。在.net里,delegate可以实现类似的功能。
举个大家可能碰到过的例子--进度(条)的问题。
在程序处理一个比较长的过程时,如果能显示进度,对于客户来说是非常棒的事情,但是一个比较大的程序很有可能会分层,而通常会把逻辑放到BLL层。
这个时候就有一个问题,如何把程序的进度显示给客户呢?答案就是用delegate。
具体说说吧。
假设你想合并两个文件。
步骤是:
Step1: 读取文件1
Step 2: 读取文件2
Step 3:合并文件1 和文件2
如何把这些信息提示给用户呢?
具体实现如下(伪代码)
A) BLL层
1
public class Combinator
2
{
3
public string FilePath1;
4
public string FilePath2;
5
public SetProgressText OnProgressText;
6
7
public void Process()
8
{
9
this.OnProgressText(" Loading" +FilePath1+"
")
10
Load(FilePath1);
11
this.OnProgressText(" Loading" +FilePath2+"
")
12
Load(FilePath2);
13
this.OnProgressText("Combining these 2 files
");
14
Combine();
15
this.OnProgressText("Finished combining these 2 files!");
16
}
17![](/Images/OutliningIndicators/InBlock.gif)
18
private void Load(string path)
19
{
20
//The logic to load file
21
}
22
private void Combine()
23
{
24
//The logic to combine 2 files
25
}
26
///
27
///Delegate.
28
///To set the progress text to user.
29
///
30
public delegate void SetProgressText(string text);
31
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/InBlock.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
看看Line 5, 30,这里是delegate的定义。
B) UI层
1
public class Form1
2
{
3
private Label lblStatus=new Label();
4
private Button btnCombine=new Button();
5
private void btnCombine_click(Object o,EventArgs e)
6
{
7
Combinator combine=new Combinator();
8
combine.OnSetProgressText=new SetProressText(SetStatus);
9
combine.Process();
10
}
11
private void SetStatus(string text)
12
{
13
this.lblStatus.Text=text;
14
}
15
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/InBlock.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
看看以上代码的Line8,这里需要给Combinator的OnSetProgressText Delegate一下,指向SetStatus就可以了。注意,SetStatus的参数定义必须和SetProgress的定义相同,不然就不叫delegate,当然编译也通不过。
这是delegate的一个简单应用,但是很有帮助哟。
更新:
Delegate可以传递的,具体如下: 假设A类调用B类,调用了C,而C类又定义了Delegate。可以在B类里定义C类的Delegate属性,然后在A类定义B类的delegate的目标函数,即实现方法。其实A类的实现函数就是C的delegate的目标函数。
Last Updated 2006年3月23日