微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否有更简洁的方法在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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐