注册对象创建事件 [英] Signing up for object created event

查看:75
本文介绍了注册对象创建事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以注册创建特定类型的对象时触发的事件,而不修改该对象? 例如,在创建Label的每个实例(将Label的实例作为参数传递)时,对Label类型的事件进行签名都会触发该事件.

Is it possible to sign up for the event that fires when the object of the specific type is created, not modifying the object? for example, signing for this event of Label type fires it when every instance of Label is created (passing the instance of Label as parameter)

对不起,我的英语不好

推荐答案

最好使用Factory模式完成此操作.如果将Label对象的创建包装在LabelFactory中,则Label Factory也可能会公开一个事件,该事件在每次创建对象时都会触发.

This would be best accomplished using a Factory pattern. If you wrap the creation of your Label objects in a LabelFactory, then the Label Factory could also expose an event that gets fired every time an object is created.

类似这样的东西:

public class LabelFactory    
{    
    public Label GetLabel()  {...}    

    public event LabelEventHandler LabelCreated;
}    

这篇关于注册对象创建事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆