是否有更简洁的方法在C#3和C#4中定义自定义事件?

在以下控制台应用程序示例中,事件定义如下:

public delegate void PurchaseHandler(object obj,PurchaseArgs args);
public event PurchaseHandler OnPurchaseMade;

在看完之后我觉得这可能有点“C#2”.

是否有更简洁的方式来表达C#3和C#4?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestEvents288202
{
    class Program
    {
        static void Main(string[] args)
        {
            Product product1 = Product.LoadProduct(222);
            EmailManager.NotifyAdministrator(product1);
            product1.OnPurchaseMade += new Product.PurchaseHandler(NotifyUser);
            product1.Purchase();

            Product product2 = Product.LoadProduct(333);
            EmailManager.NotifyAdministrator(product2);
            product2.OnPurchaseMade += new Product.PurchaseHandler(NotifyUser);
            product2.Purchase();

            Console.ReadLine();
        }

        static void NotifyUser(object sender,PurchaseArgs e)
        {
            ((Product)sender).Log();
            Console.WriteLine(e.Message);
        }
    }

    public static class EmailManager
    {
        public static void NotifyAdministrator(Product product)
        {
            product.OnPurchaseMade += new Product.PurchaseHandler(SendEmail);
        }

        public static void SendEmail(object sender,PurchaseArgs e)
        {
            Product product = sender as Product;
            Console.WriteLine("Just sent e-mail to administrator notifying of purchase of article {0}",product.ProductNumber);
        }
    }

    public class PurchaseArgs : EventArgs
    {
        public string Message { get; set; }

        public PurchaseArgs(string message)
        {
            Message = message;
        }
    }

    public class Product
    {
        public int ProductNumber { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public delegate void PurchaseHandler(object obj,PurchaseArgs args);
        public event PurchaseHandler OnPurchaseMade;

        public static Product LoadProduct(int productNumber)
        {
            List<Product> products = new List<Product>();
            products.Add(new Product { ProductNumber = 111,Name = "Intel cpu",Description = "Newest model,very fast." });
            products.Add(new Product { ProductNumber = 222,Name = "Philips Monitor",Description = "22-inch,very nice." });
            products.Add(new Product { ProductNumber = 333,Name = "Sony Camera",Description = "10 Megapixels,sharp pictures." });

            return products.Where(p => p.ProductNumber == productNumber).SingleOrDefault();
        }

        public void Purchase()
        {
            PurchaseArgs purchaseArgs = new PurchaseArgs(String.Format("The product \"{0}\" was just purchased.",this.Name));
            OnPurchaseMade(this,purchaseArgs);
        }

        public void Log()
        {
            Console.WriteLine("Log: #{0} purchased.",this.ProductNumber);
        }
    }
}

解决方法

始终定义这样的事件,不要使用自定义委托:

event EventHandler<EventArgsClasstype> MyEventHandler;

或者,如果他们不参数:

event EventHandler MyEventHandler;

通过基于System.EventHandler类,这确保了遵循.NET准则的所有事件的统一签名.

如果您的事件需要进一步的参数,则其EventArgsClasstype必须从System.EventArgs继承.

此外,在实例化事件处理程序时,您不必使用此显式形式:

product1.OnPurchaseMade += new Product.PurchaseHandler(NotifyUser);

因为方法组可以隐式转换为匹配的委托.因此,以下代码也可以正常工作:

product1.OnPurchaseMade += NotifyUser;

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...