如何克隆事件处理程序 [英] How to clone event handlers

查看:57
本文介绍了如何克隆事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


我似乎应该实现ICloneable来实现我自己的克隆

对象。对我来说,关键点是基于

制作一个控制对象,它的所有事件处理程序都设置为旧的

。有没有办法做这个工作?


例如,有没有办法使用EventInfo对象获取所有事件

旧控件的处理程序运行时并设置我的新克隆控件事件

到旧控件的事件处理程序?


任何建议都表示赞赏。


问候

Hamed


你好


我正在开发一个实用工具在其他程序中重复使用。它是
我有一个Control类型的对象(一个TextBox,ComboBox等),其他的b $ b程序员在应用程序中使用它。他们可能会设置一些属性或

分配事件处理程序。我需要能够在运行时克隆操作控件




我可以使用某些对象的Clone方法(如Font,Style,String,

等..)但像Button,TextBox,ListBox这样的控件没有这个



的方法。


如何为TextBox,

ListBox,ListViews,CheckBox等控件创建对象实例的克隆?
$ b感谢任何帮助,


解决方案

问题是EventInfo为您提供有关访问者的信息,

bot支持字段 - 即通常:


private EventHandler someEvent; //这就是我们想要的东西

公共事件EventHandler SomeEvent {//这就是EventInfo报告的内容

add {someEvent + = value;}

删除{someEvent - = value;}

}


注意:如果您输入以下内容,编译器会为您执行此操作:


公共事件EventHandler SomeEvent;


(注意不能依赖该字段的名称)。因此,如果您拥有Control和该特定事件,那么您可以轻松地执行此操作,因为您可以看到支持委托字段的可见性,但对于外部

控制或子类中的事件,要困难得多。你会非常需要步行私人领域。


另外:你应该只是克隆那些事件处理程序,你需要$

拥有,因为其他代码(比你的)我订阅的不是
期待那个电话(例如数据绑定实现等)。


我最好的建议通常是简单地重新应用你知道的事件处理程序,大概是在你使用的相同代码块中

设置原件在第一位(这可能意味着*不是*

设计者生成的代码)。在你的情况下,这可能是不可能的(因为

不是事件的setter-upper);也许有点

误导将是有序的?即你的代码订阅了所有控件的事件

,他们的代码订阅了你的代码?而且你的代码

管理克隆操作,这样你就可以订阅你的代码来控制那些事件......可能......我认为它可以获得非常凌乱,

虽然...


Marc


Marc

感谢您的评论。但问题是:


我有一个在我自己的DataGridColumnStyle中使用的控件。我想给程序员分配自己的事件处理程序

。然后有另外一列需要控制的列完全和以前一样(至少它的事件

是相同的)我需要克隆控件。所以我至少应该在新控件中复制

属性和事件处理程序。


所以我需要以某种方式在Clone方法中复制它们来自

我控制级别的ICloneable界面。


现在我该如何制作一个新的控件(假设一个类的TextBox

MyOwnTextBox)复制其属性和事件?


问候

Hamed


" Marc Gravell" < ma ********** @ gmail.comwrote in message

news:11 ******************** **@i42g2000cwa.googlegr oups.com ...


问题是EventInfo为您提供有关访问者的信息,

bot支持领域 - 即通常:


私有EventHandler someEvent; //这就是我们想要的东西

公共事件EventHandler SomeEvent {//这就是EventInfo报告的内容

add {someEvent + = value;}

删除{someEvent - = value;}

}


注意:如果您输入以下内容,编译器会为您执行此操作:


公共事件EventHandler SomeEvent;


(注意不能依赖该字段的名称)。因此,如果您拥有Control和该特定事件,那么您可以轻松地执行此操作,因为您可以看到支持委托字段的可见性,但对于外部

控制或子类中的事件,要困难得多。你会非常需要步行私人领域。


另外:你应该只是克隆那些事件处理程序,你需要$

拥有,因为其他代码(比你的)我订阅的不是
期待那个电话(例如数据绑定实现等)。


我最好的建议通常是简单地重新应用你知道的事件处理程序,大概是在你使用的相同代码块中

设置原件在第一位(这可能意味着*不是*

设计者生成的代码)。在你的情况下,这可能是不可能的(因为

不是事件的setter-upper);也许有点

误导将是有序的?即你的代码订阅了所有控件的事件

,他们的代码订阅了你的代码?而且你的代码

管理克隆操作,这样你就可以订阅你的代码来控制那些事件......可能......我认为它可以获得非常凌乱,

虽然...


Marc



再次 - 这取决于你是否拥有这些事件;这可以很简单:


public MyTextBox Clone(){

MyTextBox tb = new MyTextBox(); //在ctor中加入anthing

是readonly

//我们要复制的字段和属性...

tb.PropertyA = this.PropertyA; //如果可以访问等

tb.fieldB = this.fieldB; //在此类型中声明(不在

基类中),或受保护

tb.eventBackerC = this.eventBackerC; //在此类型中声明(不在

基类中)

tb.SimpleEventD = this.SimpleEventD; //在此类型中声明(不在

a基础级别)

}


如果你*不*拥有它们(即在基类中声明,并且没有

受保护的访问权限),那么你需要:(最好)重新考虑

设计,或者b:使用现场反思。


如果我错过了这一点,请告诉我....


Marc


Hello

It seems that I should implement ICloneable to implement my own clone
object. the critical point for me is to make a control object based on
another control object that all of its event handlers are set like the old
one. Is there a way to do this job?

For example, is there a way to use EventInfo object to get all event
handlers of the old control in runtime and set my new cloned control events
to the event handlers of the old control?

Any suggestion is appreciated.

Regards
Hamed

Hello

I am developing a utility to be reused in other programs. It

I have an object of type Control (a TextBox, ComboBox, etc.) that other
programmers use it in applications. they may set some of properties or
assign event handlers. I need to be able to clone the manipulated control
at runtime.

I could use the Clone method of some objects (like Font, Style, String,
etc..) but the controls like Button, TextBox, ListBox doesn''t have this
kind
of method.
How can I create a clone of an object instance for controls like TextBox,
ListBox, ListViews, CheckBox ??
Any help is appreciated,


解决方案

The problem is that the EventInfo gives you info about the accessor,
bot the backing field - i.e. typically:

private EventHandler someEvent; // this is what we want
public event EventHandler SomeEvent { // this is what EventInfo reports
add {someEvent += value;}
remove {someEvent -= value;}
}

note: the compiler does this for you if you type:

public event EventHandler SomeEvent;

(noting that the name of the field can''t be relied upon). So if you own
the Control and that specifc event, then you can do it easily as you
have visibility of the backing delegate field, but for external
Controls, or events in sub-classes, it is much harder. You would pretty
much need to walk the private fields.

Also: you should only really be cloning those event-handlers that you
own, since other code (than yours) my be subscribing that isn''t
expecting that call (e.g. databinding implementations, etc).

My best advice normally is to simply re-apply the event handler s that
you know about, presumably in the same block of code that you use to
set the originals up in the first place (which probably means *not* the
designer-generated code). In your case, this may not be possible (due
to not being the setter-upper of the events); perhaps a bit of
misdirection would be in order? i.e. your code subscribes to the events
of all the controls, and their code subscribes to yours? And your code
manages the Clone operation, such that you can subscribee your code to
the Control''s events... possibly... I think it could get very messy,
though...

Marc


Marc
Thanks for your comment. But the problem is:

I have a control that use in a DataGridColumnStyle of my own. I want to give
the programmer the ability to assign its own event handlers. then there is
another column that needs a control exactly as previous (at least its events
are the same) and I need to clone the control. so I should at least copy the
properties and event handlers in the new control.

So I need to somehow copy them in the Clone method that is come from
ICloneable interface of my control''s class.

Now how can I make a new control (suppose a TextBox from a class
MyOwnTextBox) that copies its properties and events?

Regards
Hamed

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...

The problem is that the EventInfo gives you info about the accessor,
bot the backing field - i.e. typically:

private EventHandler someEvent; // this is what we want
public event EventHandler SomeEvent { // this is what EventInfo reports
add {someEvent += value;}
remove {someEvent -= value;}
}

note: the compiler does this for you if you type:

public event EventHandler SomeEvent;

(noting that the name of the field can''t be relied upon). So if you own
the Control and that specifc event, then you can do it easily as you
have visibility of the backing delegate field, but for external
Controls, or events in sub-classes, it is much harder. You would pretty
much need to walk the private fields.

Also: you should only really be cloning those event-handlers that you
own, since other code (than yours) my be subscribing that isn''t
expecting that call (e.g. databinding implementations, etc).

My best advice normally is to simply re-apply the event handler s that
you know about, presumably in the same block of code that you use to
set the originals up in the first place (which probably means *not* the
designer-generated code). In your case, this may not be possible (due
to not being the setter-upper of the events); perhaps a bit of
misdirection would be in order? i.e. your code subscribes to the events
of all the controls, and their code subscribes to yours? And your code
manages the Clone operation, such that you can subscribee your code to
the Control''s events... possibly... I think it could get very messy,
though...

Marc



Again - it depends if you own the events; this could be as simple as:

public MyTextBox Clone() {
MyTextBox tb = new MyTextBox(); // include anthing in the ctor that
is "readonly"
// fields and properties that we want to copy...
tb.PropertyA = this.PropertyA; // if accessible etc
tb.fieldB = this.fieldB; // declared in this type (not in a
base-class), or protected
tb.eventBackerC = this.eventBackerC; // declared in this type (not in
a base-class)
tb.SimpleEventD = this.SimpleEventD; // declared in this type (not in
a base-class)
}

If you *don''t* own them (i.e. declared in a base class, and no
protected access), then you need to either a: (preferably) rethink the
design, or b: use field-level reflection.

Let me know if I missed the point....

Marc


这篇关于如何克隆事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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