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

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

问题描述

我无法深度复制 UserControl,因为它们没有被标记为可序列化.

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

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

What is the reason behind this design?

推荐答案

序列化一个控件不是问题,它是反序列化,这是非常困难的事情.期望这将产生一个精确的控件克隆.这几乎不可能准确地做到,有大量的运行时状态与控件相关联.不仅在Control类对象本身,还在窗口的内部状态中,声明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 窗口类名和秘密属性访问器密钥,从程序的一次运行到另一次运行是不同的.因此,不可能在之前运行的程序或完全另一个程序中序列化控件时重新创建控件.

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 设计器实际上支持控件序列化.不是字节,它生成代码.在运行时重新创建控件的代码,看起来与设计时相同.减去一大堆细节,比如尺寸和颜色,它们在另一台机器上通常会有所不同.设计器的一大优势是它只需要序列化控件的初始状态,即其在构造函​​数时的状态.在此之后的任何时候做同样的事情,在 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天全站免登陆