当我使用XmlSerialiser对其进行反序列化时,为什么我的DbNull不是单例?

时间:2021-12-07 17:20:40

I've always assumed that DbNull.value was a singleton. And thus you could do things like this:

我一直认为DbNull.value是一个单身人士。因此你可以做这样的事情:

VB.NET:

If someObject Is DbNull.Value Then
    ...
End if

C#:

If (someObject == DbNull.Value)
{
    ...
}

But recently, I serialised a DbNull instance using the XmlSerialiser and suddenly it wasn't a singleton any more. Type comparison operations (like C#'s (obj is DBNull)) work OK though.

但是最近,我使用XmlSerialiser序列化了一个DbNull实例,突然间它不再是单例了。类型比较操作(如C#(obj是DBNull))工作正常。

Code follows:

[Serializable, System.Xml.Serialization.XmlInclude(typeof(DBNull))]
public class SerialiseMe
{
    public SerialiseMe() { }

    public SerialiseMe(object value)
    {
        this.ICanBeDbNull = value;
    }
    public Object ICanBeDbNull { get; set; }
}

public void Foo()
{
    var serialiseDbNull = new SerialiseMe(DBNull.Value);
    var serialiser = new System.Xml.Serialization.XmlSerializer(typeof(SerialiseMe));
    var ms = new System.IO.MemoryStream();
    serialiser.Serialize(ms, serialiseDbNull);
    ms.Seek(0, System.IO.SeekOrigin.Begin);
    var deSerialisedDbNull = (SerialiseMe)serialiser.Deserialize(ms);

    // Is false, WTF!
    var equalsDbNullDeserialised = deSerialisedDbNull.ICanBeDbNull == DBNull.Value;
    // Is false, WTF!
    var refEqualsDbNullDeserialised = object.ReferenceEquals(deSerialisedDbNull.ICanBeDbNull, DBNull.Value);
    // Is true.
    var convertIsDbNullDeserialised = Convert.IsDBNull(deSerialisedDbNull.ICanBeDbNull);
    // Is true.
    var isIsDbNullDeserialised = deSerialisedDbNull.ICanBeDbNull is DBNull;

}

Why is this the case? And how does it happen? And can it possibly happen with any other static fields?

为什么会这样?它是如何发生的?它可能发生在任何其他静态字段中吗?

PS: I am aware the VB code sample is doing a reference comparison and c# is calling Object.Equals. Both have the same behaviour with DBNull. I usually work with VB.

PS:我知道VB代码示例正在进行参考比较,而c#正在调用Object.Equals。两者都与DBNull具有相同的行为。我通常使用VB。

2 个解决方案

#1


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.

虽然DBNull.Value是一个静态只读,并且只作为单个实例存在...当您反序列化时,序列化代码将从流中的“数据”创建类DBNull的新实例。由于DBNull.Value只是一个DBNull实例,因此序列化无法知道它是一个“特殊”实例。

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.

注意:出于同样的原因,如果您使用序列化然后反序列化的“单例”实例创建自己的类,您将获得完全相同的行为。虽然反序列化的实例与原始实例无法区分,但它们不是同一个实例。

#2


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

你的c#代码不等于调用.Equals方法。没有经过测试我真的很确定你是否替换

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

它会给你预期的结果。对于一些关于等式运算符和Equals方法的内容,请看一下:Eric Lipperts关于该主题的博客文章

#1


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.

虽然DBNull.Value是一个静态只读,并且只作为单个实例存在...当您反序列化时,序列化代码将从流中的“数据”创建类DBNull的新实例。由于DBNull.Value只是一个DBNull实例,因此序列化无法知道它是一个“特殊”实例。

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.

注意:出于同样的原因,如果您使用序列化然后反序列化的“单例”实例创建自己的类,您将获得完全相同的行为。虽然反序列化的实例与原始实例无法区分,但它们不是同一个实例。

#2


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

你的c#代码不等于调用.Equals方法。没有经过测试我真的很确定你是否替换

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

它会给你预期的结果。对于一些关于等式运算符和Equals方法的内容,请看一下:Eric Lipperts关于该主题的博客文章