Why isn't my DbNull a singleton when I deserialise it using XmlSerialiser?

烈酒焚心 提交于 2019-12-01 04:58:34

Although DBNull.Value is a static readonly and only exists as a single instance... when you de-serialize, the serialization code would be creating a new instance of the class DBNull from the 'data' in the stream. Since the DBNull.Value is simply a DBNull instance, there is no way for serialization to know that it is a 'special' instance.

NOTE:
For the same reason, if you make your own class with a 'singleton' instance that you serialize and then de-serialize you will get exactly the same behaviour. Although the deserialized instance will be indistinguishable from the original instance, they will not be the same instance.

Your c# code does not equal calling the .Equals method. With out having tested it Im actually pretty sure if you substituted

someObject == DbNull.Value

with

DbNull.Value.Equals(someObject) 

it would give you the expected result. For some insides on the equality operator and the Equals method take a look at: Eric Lipperts blog post on that subject

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