6

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

VB.NET:

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

C#:

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

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

代码如下:

[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;

}

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

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

4

2 回答 2

7

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.

于 2009-06-04T05:14:11.923 回答
1

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

于 2009-06-04T06:50:00.847 回答