如何克隆refrenced类型的对象? [英] How to clone a refrenced type object?

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

问题描述

您好


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

分配事件处理程序。我需要能够在

运行时克隆操作控件。


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

等..)但像Button,TextBox,ListBox这样的控件没有这种方法。

方法。

我怎么能为像TextBox这样的控件创建一个对象实例的克隆,

ListBox,ListViews,CheckBox ??

任何帮助表示赞赏,

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,

推荐答案

Hamed,


如果它是可序列化的,你可以序列化并反序列化。

< a rel =nofollowhref =http://www.vb-tips.com/dbPages.aspx?ID=7ffd296f-9e81-47e6-88dc-61641f5c8d9d\"target =_ blank> http://www.vb -tips.com/dbPages.aspx?...c-61641f5c8d9d

我希望这会有所帮助,


Cor


" Hamed" < ha ******* @ yahoo.comschreef in bericht

news:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
Hamed,

If it is serializable you can serialize it and deserialize it.

http://www.vb-tips.com/dbPages.aspx?...c-61641f5c8d9d

I hope this helps,

Cor

"Hamed" <ha*******@yahoo.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...

你好


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

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




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

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



的方法。


如何为TextBox,

ListBox,ListViews,CheckBox等控件创建对象实例的克隆?
$ b感谢任何帮助,
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,



你好


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

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

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

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


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

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

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


任何建议都表示赞赏。


问候

Hamed
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

你好


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

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




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

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



的方法。


如何为TextBox,

ListBox,ListViews,CheckBox等控件创建对象实例的克隆?
$ b感谢任何帮助,
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,



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

支持领域 - 即通常:


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

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

add {someEvent + = value;}

删除{someEvent - = value;}

}


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


公共事件EventHandler SomeEvent;


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

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


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

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


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

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

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

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

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

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

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

虽然...


Marc

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


这篇关于如何克隆refrenced类型的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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