简体   繁体   English

Vb.net 9 中的产量

[英]Yield in Vb.net 9

I want to use the C# yield in VB.net 9 but it is not working.我想在 VB.net 9 中使用 C# yield 但它不起作用。

private static IEnumerable<JToken> AllChildren(JToken json)
{
    foreach (var c in json.Children())
    {
        yield return c;
        foreach (var cc in AllChildren(c))
        {
            yield return cc;
        }
    }
}

this is the c# code and it is converted to VB.net using the online tools but it is not giving me the same result.这是 c# 代码,它使用在线工具转换为 VB.net,但它没有给我相同的结果。

Private Shared Function AllChildren(json As JToken) As IEnumerable(Of JToken)
For Each c As var In json.Children()
    yield Return c
    For Each cc As var In AllChildren(c)
        yield Return cc
    Next
Next
End Function

Can anybody help me translate this?有人可以帮我翻译一下吗?

It took me a while to get it right, but this is what you need:我花了一段时间才把它弄好,但这就是你所需要的:

Private Shared Iterator Function AllChildren(json As JToken) As IEnumerable(Of JToken)
    For Each c As JToken In json.Children()
        Yield c
        For Each cc As JToken In AllChildren(c)
            Yield  cc
        Next
    Next
End Function

Since you are working in VB9 and you can't use iterators there is a very easy and neat option that you can use.由于您在 VB9 中工作并且不能使用迭代器,因此您可以使用一个非常简单和简洁的选项。

You just need to NuGet "Ix-Main" to get the Microsoft Reactive Framework team's "Interactive Extensions" - a bunch of handy IEnumerable<T> operators.你只需要 NuGet "Ix-Main" 来获得 Microsoft Reactive Framework 团队的 "Interactive Extensions" - 一堆方便的IEnumerable<T>操作符。

Then you can try this code:然后你可以试试这个代码:

Private Shared Function AllChildren2(json As JToken) As IEnumerable(Of JToken)
    Return EnumerableEx.Expand(json.Children(), Function (c) c.Children())
End Function

Create a C# class project, add json.net package/reference to the project.创建一个 C# 类项目,将 json.net 包/引用添加到项目中。 Add the following class添加以下类

using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace NewtonExtensions
{
    public static class Extensions
    {
        private static IEnumerable<JToken> AllChildren(JToken json)
        {
            foreach (var c in json.Children())
            {
                yield return c;
                foreach (var cc in AllChildren(c))
                {
                    yield return cc;
                }
            }
        }
    }
}

Compile the above project.编译上面的项目。 In the current project, add a reference to the project above.在当前项目中,添加对上述项目的引用。 Where the extension method is to be used, add a using statement.在要使用扩展方法的地方,添加 using 语句。

Underlying thing here is unless using an older Express version of Visual Studio we can easily share code between languages.这里的基础是,除非使用较旧的 Express 版本的 Visual Studio,否则我们可以轻松地在语言之间共享代码。

EDIT Here is the same as above as an extension method编辑这里与上面的扩展方法相同

using Newtonsoft.Json.Linq;使用 Newtonsoft.Json.Linq; using System.Collections.Generic;使用 System.Collections.Generic;

namespace NewtonExtensions
{
    public static class Extensions
    {
        public static IEnumerable<JToken> AllChildren(this JToken json)
        {
            foreach (var c in json.Children())
            {
                yield return c;
                foreach (var cc in AllChildren(c))
                {
                    yield return cc;
                }
            }
        }
    }
}

Example usage in vb.net where I use xml literal for the json string. vb.net 中的示例用法,其中我对 json 字符串使用 xml 文字。 You would read in the json as normally would say from a file etc.您会像通常从文件等中所说的那样读取 json。

The Imports NewtonExtensions is in a C# class project with a namespace of NewtonExtensions. Imports NewtonExtensions 位于命名空间为 NewtonExtensions 的 C# 类项目中。

I show two methods to iterate data after using the language extension method.在使用语言扩展方法后,我展示了两种迭代数据的方法。

Imports NewtonExtensions
Imports Newtonsoft.Json.Linq

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) _
        Handles Button1.Click

        Dim jsonString As String =
            <json>
{
    "ADDRESS_MAP":{

        "ADDRESS_LOCATION":{
            "type":"separator",
            "name":"Address",
            "value":"",
            "FieldID":40
        },
        "LOCATION":{
            "type":"locations",
            "name":"Location",
            "keyword":{
                "1":"LOCATION1"
            },
            "value":{
                "1":"United States"
            },
            "FieldID":41
        },
        "FLOOR_NUMBER":{
            "type":"number",
            "name":"Floor Number",
            "value":"0",
            "FieldID":55
        },
        "self":{
            "id":"2",
            "name":"Address Map"
        }
    }
}                
            </json>.Value


        Dim results As JObject = JObject.Parse(jsonString)

        Console.WriteLine("Example 1")
        For Each item As KeyValuePair(Of String, JToken) In results
            Console.WriteLine(item.Key)
            Dim test = item.Value.AllChildren
            For Each subItem In test
                Console.WriteLine(subItem)
                Console.WriteLine()
            Next
        Next
        Console.WriteLine(New String("-"c, 50))
        Console.WriteLine("Example 2")
        results.Cast(Of KeyValuePair(Of String, JToken)) _
            .ToList.ForEach(
                Sub(v)
                    Console.WriteLine(v.Key)
                    Console.WriteLine(v.Value)
                End Sub)


    End Sub
End Class

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

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