繁体   English   中英

C#中的VB.NET委托

[英]A delegate in C# to VB.NET

我正在尝试使用NUnit 2.6.4将此工作的.NET2 C#转换为.NET2 VB.NET

[Test]
public void Test() {
    Assert.Throws<Exception>(delegate {
        DoSomething(2);
    });
}

public void DoSomething(int i) {
    throw new Exception();
}

然后进入VB:

<Test> _
Public Sub Test()
    Assert.Throws(Of Exception)(Sub() DoSomething(2))
End Sub

Public Sub DoSomething(i As Integer)
    Throw New Exception()
End Sub

这适用于.NET4但不适用于.NET2-因为我正在使用匿名方法...但是如何在VB.NET中的.NET2中表达委托

类似于: AddressOf DoSomething(2)

您可以创建一个新的命名方法,而不是使用匿名方法。

Public Sub Test()
    Assert.Throws(Of Exception)(AddressOf DoSomethingPassingTwo)
End Sub

Public Sub DoSomethingPassingTwo()
    DoSomething(2)
End Sub

Public Sub DoSomething(i As Integer)
    Throw New Exception()
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM