Csharp/C#教程:VB.Net中的匿名类初始化分享


VB.Net中的匿名类初始化

我想在vb.net中创建一个完全像这样的匿名类:

var data = new { total = totalPages, page = page, records = totalRecords, rows = new[]{ new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}}, new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}}, new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}} } }; 

谢谢。

VB.NET 2008没有new[]结构,但是VB.NET 2010没有。 你不能直接在VB.NET 2008中创建一个匿名类型数组。诀窍是声明一个这样的函数:

 Function GetArray(Of T)(ByVal ParamArray values() As T) As T() Return values End Function 

并让编译器为我们推断出类型(因为它是匿名类型,我们不能指定名称)。 然后使用它像:

 Dim jsonData = New With { _ .total = totalPages, _ .page = page, _ .records = totalRecords, _ .rows = GetArray( _ New With {.id = 1, .cell = GetArray("1", "-7", "Is this a good question?")}, _ New With {.id = 2, .cell = GetArray("2", "15", "Is this a blatant ripoff?")}, _ New With {.id = 3, .cell = GetArray("3", "23", "Why is the sky blue?")} ) _ } 

PS。 这不称为JSON。 它被称为匿名类型。

在VS2010中:

上述就是C#学习教程:VB.Net中的匿名类初始化分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 Dim jsonData = New With { .total = 1, .page = Page, .records = 3, .rows = { New With {.id = 1, .cell = {"1", "-7", "Is this a good question?"}}, New With {.id = 2, .cell = {"2", "15", "Is this a blatant ripoff?"}}, New With {.id = 3, .cell = {"3", "23", "Why is the sky blue?"}} } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/949807.html

(0)
上一篇 2021年11月18日
下一篇 2021年11月18日

精彩推荐