.NET 多播委托






4.29/5 (24投票s)
多播委托提供执行多个方法的功能。
引言
多播委托提供执行多个方法的功能。
在内部,存储了一个委托的链表(称为调用列表),当调用多播委托时,委托列表将按顺序执行。
如何…
开始使用多播委托的最简单方法是创建单个委托并将它们组合成一个多播委托,如以下代码片段所示
/// Declares the multicast delegate
public delegate void myDel();
static public void Main(string[] args)
{
/// Declares the single delegate that points to MethodA
myDel myDelA = new myDel(MethodA);
/// Declares the single delegate that points to MethodA
myDel myDelB = new myDel(MethodB);
/// Declares the multicast delegate combining both delegates A and B
myDel myMultiCast = (myDel)Delegate.Combine(myDelA, myDelB);
/// Invokes the multicast delegate
myMultiCast.Invoke();
}
static void MethodA()
{
Console.WriteLine(“Executing method A.”);
}
static void MethodB()
{
Console.WriteLine(“Executing method B.”);
}
输出如下
Executing method A.
Executing method B.
从 MulticastDelegate 类派生类
Delegate
和 MulticastDelegate
类不能显式派生,但有一种方法可以做到。
以下示例定义了三个类,名为 Order
、Stock
和 Receipt
,除了控制台应用程序中的主方法之外。
Order
类持有定义方法签名的委托,并提供将产品项目添加到订单的方法,以及一个结账方法,该方法将委托作为参数来定义要调用的方法。它可以用于单个委托或多播委托。Stock
类具有从库存中删除产品的功能。Receipt
类具有将项目打印到收据的功能。Main
方法创建了Order
类型的一个实例,将产品项目添加到订单中,并基于两个派生委托的组合创建了多播委托。
让我们检查代码
class Order
{
/// Main order delegate
public delegate void myOrderDel(int prodId, int quantity);
/// Stores a dictionary of products ids and respective quantities
private static HybridDictionary prodList = new HybridDictionary();
public void AddItem(int prodId, int quantity)
{
/// Add products and quantitites to the dictionary.
prodList.Add(prodId, quantity);
}
public static void Checkout(myOrderDel multicastDelegate)
{
/// Loop through all products in the dictionary
foreach (DictionaryEntry prod in prodList)
{
/// Invoke the multicast delegate
multicastDelegate.Invoke(Convert.ToInt32(prod.Key),
Convert.ToInt32(prod.Value));
}
}
}
class Stock
{
public static void Remove(int prodId, int quantity)
{
Console.WriteLine(“{0} unit(s) of the product {1} has/have" +
" been removed from the stock.”, quantity, prodId);
}
}
class Receipt
{
public static void PrintItem(int prodId, int quantity)
{
Console.WriteLine(“{0} unit(s) of the product {1} has/have been" +
" printed to the receipt.”, quantity, prodId);
}
}
static public void Main(string[] args)
{
/// Create the order object
Order myOrder = new Order();
/// Add products and quantities to the order
myOrder.AddItem(1, 2);
myOrder.AddItem(2, 3);
myOrder.AddItem(3, 1);
myOrder.AddItem(4, 1);
myOrder.AddItem(5, 4);
/// Order delegate instance pointing to Stock class.
Order.myOrderDel myStockDel = new Order.myOrderDel(Stock.Remove);
/// Receipt delegate instance pointing to Receipt class.
Order.myOrderDel myReceiptDel = new Order.myOrderDel(Receipt.PrintItem);
/// Combine the two previous delegates onto the multicast delegate.
Order.myOrderDel myMulticastDel =
(Order.myOrderDel)Delegate.Combine(myStockDel, myReceiptDel);
/// Invoke the checkout method passing the multicast delegate
Order.Checkout(myMulticastDel);
}
输出如下
2 unit(s) of the product 1 has/have been removed from the stock.
2 unit(s) of the product 1 has/have been printed to the receipt.
3 unit(s) of the product 2 has/have been removed from the stock.
3 unit(s) of the product 2 has/have been printed to the receipt.
1 unit(s) of the product 3 has/have been removed from the stock.
1 unit(s) of the product 3 has/have been printed to the receipt.
1 unit(s) of the product 4 has/have been removed from the stock.
1 unit(s) of the product 4 has/have been printed to the receipt.
4 unit(s) of the product 5 has/have been removed from the stock.
4 unit(s) of the product 5 has/have been printed to the receipt.
MSDN 网站提供了另一个很好的示例,说明如何从 MulticastDelegate
类(并非以显式方式)派生一个类。 点击这里阅读!