处理委托的 C# 语法在 VB 中的等价物是什么?

发布于 2024-08-12 03:30:52 字数 1732 浏览 2 评论 0原文

是否可以使用 VB 9.0 将以下 C# 代码转换为 VB.NET?

delegate Stream StreamOpenerDelegate(String name);

void Exec1()
{
    WorkMethod( x => File.OpenRead(x));
}

void Exec2()
{
    StreamOpenerDelegate opener = x => return File.OpenRead(x) ;
    WorkMethod(opener);
}

我可以做这样的事情吗?:

Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream

Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
    ''
End Sub

Private Sub Exec1()
    Me.WorkMethod(Function (ByVal x As String) 
        Return File.OpenRead(x)
    End Function)
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (ByVal x As String) 
        Return File.OpenRead(x)
    End Function
    Me.WorkMethod(opener)
End Sub

我正在尝试编写一些文档,但我不知道 VB 语法。我经常使用 Reflector 来翻译它,但我不确定它是否适用这个案例。我也不清楚哪里需要行连续字符。


回答
在 VB9 中,不可能有多行 lambda(或 Sub lambda,我没有询问过)。在 VB9 中,所有 lambda 都返回一个值,并且必须是单个表达式。这在 VB10 中发生了变化。 VB10 将允许上述语法,但 VB9 不允许。在VB9中,如果逻辑涉及多行代码,则一定不能是lambda;您必须将其放入命名函数中并显式引用它。像这样:

Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream

Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
    ''
End Sub

Function MyStreamOpener(ByVal entryName As String) As Stream
    '' possibly multiple lines here
    Return File.OpenRead(entryName)
End Function

Private Sub Exec1()
    Me.WorkMethod(AddressOf MyStreamOpener)
End Sub

站点:迈克·麦金泰尔的博客

Is it possible to translate the following C# code into VB.NET, using VB 9.0?

delegate Stream StreamOpenerDelegate(String name);

void Exec1()
{
    WorkMethod( x => File.OpenRead(x));
}

void Exec2()
{
    StreamOpenerDelegate opener = x => return File.OpenRead(x) ;
    WorkMethod(opener);
}

Can I do something like this?:

Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream

Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
    ''
End Sub

Private Sub Exec1()
    Me.WorkMethod(Function (ByVal x As String) 
        Return File.OpenRead(x)
    End Function)
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (ByVal x As String) 
        Return File.OpenRead(x)
    End Function
    Me.WorkMethod(opener)
End Sub

I'm trying to write some documentation, but I don't know VB syntax. Often I use Reflector to translate it, but I'm not sure it's working in this case. I'm also not clear on where I would need line continuation characters.


ANSWER
In VB9, it's not possible to have multi-line lambdas (or Sub lambdas, which I did not ask about). In VB9, all lambdas return a value, and must be a single expression. This changes in VB10. VB10 will allow the above syntax, but VB9 will not. In VB9, if the logic involves multiple code lines, it must not be a lambda; you must put it into a named Function and reference it explicitly. Like this:

Private Delegate Function StreamOpenerDelegate(ByVal name As String) As Stream

Private Sub WorkMethod(ByVal d As StreamOpenerDelegate)
    ''
End Sub

Function MyStreamOpener(ByVal entryName As String) As Stream
    '' possibly multiple lines here
    Return File.OpenRead(entryName)
End Function

Private Sub Exec1()
    Me.WorkMethod(AddressOf MyStreamOpener)
End Sub

site: Mike McIntyre's blog

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2024-08-19 03:30:52

这应该有效:

Private Sub Exec1()
    Me.WorkMethod(Function (x) File.OpenRead(x))
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (x) File.OpenRead(x)

    Me.WorkMethod(opener)
End Sub

您需要行继续符将单行语句拆分为多行,如下所示:

Private Sub Exec1()
    Me.WorkMethod(Function (x) _
                    File.OpenRead(x))
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (x) _
                                           File.OpenRead(x)

    Me.WorkMethod(opener)
End Sub

在任何情况下,在 VS2010 在某些字符后有隐式续行。所以我不会太担心。

This should work:

Private Sub Exec1()
    Me.WorkMethod(Function (x) File.OpenRead(x))
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (x) File.OpenRead(x)

    Me.WorkMethod(opener)
End Sub

You need the line continuation character to split a single line statement into multiple lines, like so:

Private Sub Exec1()
    Me.WorkMethod(Function (x) _
                    File.OpenRead(x))
End Sub

Private Sub Exec2()
    Dim opener As StreamOpenerDelegate = Function (x) _
                                           File.OpenRead(x)

    Me.WorkMethod(opener)
End Sub

In any case, in VS2010 there is implicit line continuation after certain characters. So I wouldn't worry about it too much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文