为什么System.Windows.Forms.Control的未标记为可序列化? [英] Why are System.Windows.Forms.Control not marked as serializable?

查看:1438
本文介绍了为什么System.Windows.Forms.Control的未标记为可序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够深拷贝用户控件,因为它们没有标记为可序列化。

I am not able to deep copy UserControls because they are not marked as serializable.

这是什么设计背后的原因是什么?

What is the reason behind this design?

推荐答案

序列化的控制是没有问题的,这是的反序列化的是这是非常努力,做正确的。人们期望的是,这将产生控制的的确切的克隆。这几乎是不可能做到准确的,还有的运行状态与控制相关的大量。不只是在控制类对象本身,也可以在窗口的内部状态,状态Windows不允许你直接访问。

Serializing a control isn't the problem, it is deserializing it that's incredibly hard to do right. The expectation is that this will produce an exact clone of the control. That's almost impossible to do accurately, there's an enormous amount of runtime state associated with a control. Not just in the Control class object itself, also in the internal state of the window, state that Windows doesn't allow you to directly access.

但最终的问题是,它具有真实与流程实例相关状态。重要的内部属性,如Windows的窗口类名和秘密属性存取密钥是从程序到另一个一个运行不同。重塑控制,当它被序列化的程序的previous运行,或其他程序完全,因此不可能的。

But the ultimate problem is that it has state that's associated with the process instance. Important internal properties like the Windows window class name and the secret property accessor keys are different from one run of the program to another. Recreating the control when it was serialized in a previous run of the program, or another program entirely, is thus not possible.

这表示,设计师的WinForms实际上支持控制序列化。不为字节,它生成的 code 的。 code,再现控制在运行时,看起来它确实在设计时是一样的。减一大堆像大小和颜色细节,他们最后往往在另一台机器不同。该设计师的一大优势是,它仅需要序列化的初始的控制,它的构造函数时状态的状态。做同样的,在这之后的任何一点,Windows后创建了控件的窗口,送去一束消息是远远就难了。这是一个错误的工厂。 ,因此不支持。

That said, the Winforms designer actually supports control serialization. Not into bytes, it generates code. Code that recreates the control at runtime, looking the same as it did at design time. Minus a whole bunch of details like size and colors, they often end up different on another machine. The big advantage that the designer has is that it only needs to serialize the initial state of the control, its state at constructor time. Doing the same at any point after this, after Windows has created the control's window and sent it a bunch of messages is a far tougher nut to crack. It is a bug factory. And thus not supported.

这篇关于为什么System.Windows.Forms.Control的未标记为可序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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