问题
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