fbInstance.Invoke(1); 


  但是,C#做了进一步优化,你可以直接通过代理对象来调用其所封装的方法,例如:


fbInstance(1); 


  三、连接和移除代理对象

  前面看到的代理对象都是封装一个方法,实际上,代理对象可以将一系列方法封装成一个链表,这一特性取决于Delegate类的Combine和Remove方法。


fbChain = (Feedback) Delegate.Combine(fbChain, fb1);


  实际上,C#对这一用法也提供了优化,可以使用+、+=、-、-=运算符来添加或移除代理对象:


fbChain += fb1; 


  代理的扩展:事件

  C#事件的引入类似属性的引入,属性是对字段的封装,而事件对象是对代理对象的封装。

  当定义如下事件时:


public event EventHandler NewMail;


  C#编译器实际上会将其转换成如下的代码:


// 1. A PRIVATE delegate field that is initialized to null
private EventHandler NewMail = null;
// 2. A PUBLIC add_Xxx method (where Xxx is the Event name)
// Allows methods to register interest in the event.
public void add_NewMail(EventHandler value) {
// The loop and the call to CompareExchange is all just a fancy way
// of adding a delegate to the event in a thread-safe way
EventHandlerprevHandler;
EventHandler newMail = this.NewMail;
do {
prevHandler = newMail;
EventHandlernewHandler =
(EventHandler) Delegate.Combine(prevHandler, value);
newMail = Interlocked.CompareExchange>(
ref this.NewMail, newHandler, prevHandler);
} while (newMail != prevHandler);
}
// 3. A PUBLIC remove_Xxx method (where Xxx is the Event name)
// Allows methods to unregister interest in the event.
public void remove_NewMail(EventHandler value) {
// The loop and the call to CompareExchange is all just a fancy way
// of removing a delegate from the event in a thread-safe way
EventHandler prevHandler;
EventHandler newMail = this.NewMail;
do {
prevHandler = newMail;
EventHandler newHandler =
(EventHandler) Delegate.Remove(prevHandler, value);
newMail = Interlocked.CompareExchange>(
ref this.NewMail, newHandler, prevHandler);
} while (newMail != prevHandler);
}

  从上面生成的代码可以看出,事件对象封装了类中的代理对象,并且只暴露出add_xxx和remove_xxx方法(事件对象的+=和-=即调用相应的方法),而外界无法对其封装的代理对象进行额外的操作(构建并初始化代理对象、调用代理对象封装的方法)。