将代码片段从C#转换为VB.NET

时间:2010-03-07 09:13:31

标签: c# vb.net code-conversion

所有自动在线转换器都无法转换此代码。不幸的是,我对C#的简要了解也让我感到失望。该代码源自一个博客,链接自another of my questions

以下是C#中的代码段;

        var virtualFileDataObject = new VirtualFileDataObject();
        virtualFileDataObject.SetData(new VirtualFileDataObject.FileDescriptor[]
        {
            new VirtualFileDataObject.FileDescriptor
            {
                Name = "abc.txt",
                StreamContents = stream =>
                    {
                        using(var webClient = new WebClient())
                        {
                            var data = webClient.DownloadData("http://www.google.com");
                            stream.Write(data, 0, data.Length);
                        }
                    }
            },
        });

我目前使用VB.NET(删除了一些内联的东西);

    Dim virtualFileDataObject = New VirtualFileDataObject()
    Dim vf As New VirtualFileDataObject.FileDescriptor()

    vf.Name = "abc.txt"
    vf.StreamContents = ??

    Using webc As New WebClient()
        Dim data = webc.DownloadData("http://www.google.com")
        stream??.Write(data, 0, data.Length)
    End Using

    virtualFileDataObject.SetData(vf)

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

StreamContents正在设置一个匿名方法,VB.NET不支持(但它在VB.NET 10中出现,它在.NET 4.0中出现)。我可以建议的最好的事情是:

vf.StreamContents = AddressOf(MyStreamContents)

Public Sub MyStreamContents(ByVal stream As <Whatever the type is>)

  Using webc As New WebClient()
        Dim data = webc.DownloadData("http://www.google.com")
        stream.Write(data, 0, data.Length)
    End Using

End Sub