简体   繁体   English

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

[英]Converting Code Snippet from C# to VB.NET

All the automated, online converters weren't able to convert this code. 所有自动化的在线转换器都无法转换此代码。 Unfortunately my brief knowledge of C# has also let me down. 不幸的是,我对C#的简短了解也让我失望。 The code originates from a blog, linked from another of my questions . 该代码来自一个博客,该博客与我的另一个问题相关联。

Here is the code snippet in C#; 这是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);
                        }
                    }
            },
        });

I currently have in VB.NET (removed some of the in-line stuff); 我目前在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)

Your help would be greatly appreciated! 您的帮助将不胜感激!

StreamContents is being set with an anonymous method, which VB.NET does not support (but it does in VB.NET 10 which is coming out in .NET 4.0). StreamContents使用匿名方法设置,VB.NET不支持该方法(但在.NET 4.0中出现的VB.NET 10中支持)。 Next best thing I can suggest is this: 我可以建议的下一个最佳方法是:

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

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

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