我有一个简单的变量声明,一个由反编译器生成的类 Friend Class Project Private _Status As Integer Public Property Status As Integer Get Return Me._Status End Get Set(ByVal value As Integer) Interlocked.Exchange(Me._Status,
Friend Class Project Private _Status As Integer Public Property Status As Integer Get Return Me._Status End Get Set(ByVal value As Integer) Interlocked.Exchange(Me._Status, value) End Set End Property End Class
这个声明是否有任何简写形式.
实际上,这是在类内部使用的backgroundworker,并由另一个类在外部访问.
要明白速记的含义是什么.我举个例子:
以下gode是速记
SyncLock lock z = 1 End SyncLock
以下详细代码
Dim obj As Object = Me.lock ObjectFlowControl.CheckForSyncLockOnValueType(obj) Dim flag As Boolean = False Try Monitor.Enter(obj, flag) Me.z = 1 Finally If (flag) Then Monitor.[Exit](obj) End If End Try正如Holterman所提到的,Interlocked.Exchange提供的唯一好处是内存障碍. (Int32赋值在.NET中始终是原子的,并且您将丢弃返回值.)
如果源代码是用C#编写的,那么它最初可能包含volatile关键字,这也会产生内存屏障.
private volatile int _Status; public int Status { get { return _Status; } set { _Status = value; } }
但是,这应该导致在getter中生成内存屏障.