简体   繁体   English

C#中的VB.NET委托

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

I'm trying to convert this working .NET2 C# into .NET2 VB.NET, using NUnit 2.6.4 我正在尝试使用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();
}

then into VB: 然后进入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

this works in .NET4 but not .NET2 - as I'm using an anonymous method... but how to express a delegate in .NET2 in VB.NET 这适用于.NET4但不适用于.NET2-因为我正在使用匿名方法...但是如何在VB.NET中的.NET2中表达委托

Something like: AddressOf DoSomething(2) 类似于: AddressOf DoSomething(2)

You can create a new named method, rather than using an anonymous method. 您可以创建一个新的命名方法,而不是使用匿名方法。

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