匿名类初始化在VB.Net [英] Anonymous class initialization in VB.Net
本文介绍了匿名类初始化在VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建在vb.net匿名类恰好是这样的:
i want to create an anonymous class in vb.net exactly like this:
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?"}}
}
};
THX。
thx.
推荐答案
VB.NET 2008不具备新[]
结构,但VB.NET 2010一样。您不能直接在VB.NET 2008年创建匿名类型的数组关键是要宣布这样的功能:
VB.NET 2008 does not have the new[]
construct, but VB.NET 2010 does. You cannot create an array of anonymous types directly in VB.NET 2008. The trick is to declare a function like this:
Function GetArray(Of T)(ByVal ParamArray values() As T) As T()
Return values
End Function
和让编译器推断为我们的类型(因为它的匿名类型,我们不能指定名称)。然后使用它像:
And have the compiler infer the type for us (since it's anonymous type, we cannot specify the name). Then use it like:
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。这就是所谓的匿名类型。
PS. This is not called JSON. It's called an anonymous type.
这篇关于匿名类初始化在VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文