Can a class derived from DependencyObject marked a

2019-09-03 14:38发布

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.

2条回答
Rolldiameter
2楼-- · 2019-09-03 15:17

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-03 15:22

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].

查看更多
登录 后发表回答