对象装箱的差异/比较C#和VB.Net之间的参考

时间:2017-11-10 12:43:30

标签: c# .net vb.net reference boxing

我今天在VB.NET中遇到了关于拳击和参考比较的行为,这是我没想到的。为了说明我编写了一个简单的程序,它试图以原子方式更新任何类型的变量。

这是C#(https://dotnetfiddle.net/VsMBrg)中的一个程序:

using System;

public static class Program
{

    private static object o3;

    public static void Main()
    {
        Console.WriteLine("Hello World");

        Test<DateTimeOffset?> value = new Test<DateTimeOffset?>();
        Console.WriteLine(value.Value == null);
        DateTimeOffset dt1 = new DateTimeOffset(2017, 1, 1, 1, 1, 1, TimeSpan.Zero);
        DateTimeOffset dt2 = new DateTimeOffset(2017, 1, 2, 1, 1, 1, TimeSpan.Zero);

        Console.WriteLine(value.TrySetValue(null, dt1));
        Console.WriteLine(value.Value == dt1);

        // this should fail
        Console.WriteLine(value.TrySetValue(null, dt2));
        Console.WriteLine(value.Value == dt1);

        // this should succeed
        Console.WriteLine(value.TrySetValue(dt1, dt2));
    }
}

public class Test<T>
{


    public T Value {
        get { return (T)System.Threading.Volatile.Read(ref _value); }
    }


    private object _value;

    public bool TrySetValue(T oldValue, T newValue)
    {
        object curValObj = System.Threading.Volatile.Read(ref _value);
        if (!object.Equals((T)curValObj, oldValue))
            return false;
        object newValObj = (object)newValue;
        return object.ReferenceEquals(System.Threading.Interlocked.CompareExchange(ref _value, newValObj, curValObj), curValObj);
    }

}

该程序的输出是:

Hello World
True
True
True
False
True
True

这是预期的,一切似乎都运行良好。 这是VB.NET中的相同程序(https://dotnetfiddle.net/lasxT2):

Imports System

Public Module Module1

    private o3 as object

    Public Sub Main()
        Console.WriteLine("Hello World")

        Dim value As New Test(Of DateTimeOffset?)
        Console.WriteLine(value.Value is nothing)
        Dim dt1 As New DateTimeOffset(2017, 1, 1, 1, 1, 1, TimeSpan.Zero)
        Dim dt2 As New DateTimeOffset(2017, 1, 2, 1, 1, 1, TimeSpan.Zero)

        Console.WriteLine(value.TrySetValue(Nothing, dt1))
        Console.WriteLine(value.Value = dt1)

        ' This should fail
        Console.WriteLine(value.TrySetValue(Nothing, dt2))
        Console.WriteLine(value.Value = dt1)

        ' This should succeed
        Console.WriteLine(value.TrySetValue(dt1, dt2))
    End Sub
End Module

public class Test(Of T)


    Public readonly Property Value As T
        Get
            Return CType(Threading.Volatile.Read(_value), T)
        End Get
    End Property

    Private _value As Object

    Public Function TrySetValue(oldValue As T, newValue As T) As Boolean
        Dim curValObj As Object = Threading.Volatile.Read(_value)
        If Not Object.Equals(CType(curValObj, T), oldValue) Then Return False
        Dim newValObj = CObj(newValue)
        Return Object.ReferenceEquals(Threading.Interlocked.CompareExchange(_value, newValObj, curValObj), curValObj)
    End Function

end class

这里的输出是:

Hello World
True
True
True
False
True
False

这里最后一个语句是false,这意味着该集合不起作用。 我在这里做错了什么,或者是VB.NET中的问题?

(注意:忽略Volatile读/写,此示例没有线程,因此不受线程影响)

编辑: 如果我将T更改为整数,那么一切正常: (dotnetfiddle.net/X6uLZs)。 此外,如果我将T更改为自定义类,它也可以正常工作:
dotnetfiddle.net/LnOOme

1 个答案:

答案 0 :(得分:4)

我认为此问题的原因实际上是VB的Object处理,在某些地方它更像C#&#39; s {{ 1}}而不是普通的dynamic。具体来说,如果我重写Object为:

TrySetValue

我们绝不会指望 Public Function TrySetValue(oldValue As T, newValue As T) As Boolean Dim curValObj As Object = _value 'Threading.Volatile.Read(_value) Console.Write(Object.ReferenceEquals(curValObj,_value)) If Not Object.Equals(CType(curValObj, T), oldValue) Then Return False Dim newValObj = CObj(newValue) Return Object.ReferenceEquals(Threading.Interlocked.CompareExchange(_value, newValObj, curValObj), curValObj) End Function 打印Console.WriteLine。但这正是它的作用。将此代码反编译回C#(使用Reflector),我获得了这段代码:

False
亲爱的,亲爱的。这些对GetObjectValue在这里做什么的电话是什么?那么,他们所拥有的效果就是使副本由盒装值类型组成,因此public bool TrySetValue(T oldValue, T newValue) { object objectValue = RuntimeHelpers.GetObjectValue(this._value); Console.Write(object.ReferenceEquals(RuntimeHelpers.GetObjectValue(objectValue), RuntimeHelpers.GetObjectValue(this._value))); if (!object.Equals(Conversions.ToGenericParameter<T>(objectValue), oldValue)) { return false; } object obj3 = newValue; return object.ReferenceEquals(RuntimeHelpers.GetObjectValue(Interlocked.CompareExchange(ref this._value, RuntimeHelpers.GetObjectValue(obj3), RuntimeHelpers.GetObjectValue(objectValue))), RuntimeHelpers.GetObjectValue(objectValue)); } 永远不会包含与curValObj相同对象的实际引用引用,所以当我们处理实际的对象引用时,_value永远不会工作。

我现在无法想到重写这段代码的好方法,可以做你想做的事。也许我们可以看到进一步的原因Interlocked.CompareExchange超载CompareExchange警告我们:

  

请勿将此重载用于值类型。