使用内联委托将C#代码转换为vb.net

时间:2019-03-23 16:03:56

标签: c# vb.net delegates

我需要将此代码转换为vb.net

public IDisposable WriteLock()
{
    EnterWriteLock();
    return new LockDisposer(delegate() { ExitWriteLock(); });
}

这是我通过在线转换器获得的

Public Function WriteLock() As IDisposable
    EnterWriteLock()
    Return New LockDisposer(Sub() ExitWriteLock()) <-- Error Line
End Function

我正在使用.net 2.0

2 个答案:

答案 0 :(得分:2)

Public Function WriteLock() As IDisposable
     EnterWriteLock()
     Return New LockDisposer(AddressOf ExitWriteLock)
End Function

答案 1 :(得分:-1)

您的代码缺少委托的结尾子:

Public Function WriteLock() As IDisposable
    Return New LockDisposer(Sub()
                                ExitWriteLock()
                            End Sub) 
End Function

Live Demo