Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

c# - Why are System.Windows.Forms.Control not marked as serializable?

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

What is the reason behind this design?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...