Can a class derived from DependencyObject marked as serializable?

天大地大妈咪最大 提交于 2019-12-25 09:42:15

问题


What des it mean by the following sentence

DependencyObjects are not marked as serializable 

To my knowledge, I can have a class which inherits from DependencyObject...and I can mark it as serializable. Anyway serialize property is not inheritable.

Could someone make me clear. Thanks in advance.


回答1:


This means that a class that derives from DependencyObject cannot be serialized with standard serializers. As a workaround you could use XamlReader/XamlWriter as shown in this article.




回答2:


DependencyObject exists to make use of the entire dependency property system. One side effect of this is that it really doesn't mesh with the approach to serialization taken in .NET.

Serialization, in .NET, serializes fields in a class. However, DependencyObjects don't actually use (instance) fields, but rather registered DependencyProperties, which are stored elsewhere (if at all). As such, they don't serialize properly, even if you mark them [Serializable].



来源:https://stackoverflow.com/questions/7294650/can-a-class-derived-from-dependencyobject-marked-as-serializable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!