作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个结构“xyz”,其中包含 3 个字符串对象。 "foo""bar"和 "abc"我想遍历结构并比较对象的名称。
Structure xyz
dim foo as string
dim bar as string
dim abc as string
End Structure
For each x as object in xyz
if x.Name = "foo" then
'bang
end if
End each
最佳答案
如果这只是一次性的事情,您可能会更轻松地使用 Dictionary 代替,但如果您更喜欢保留结构,则可以使用 Reflection 来做到这一点。
这个小代码片段将在 StringBuilder
中为您列出每个结构成员.
Dim sbOutput As New System.Text.StringBuilder
Dim t As Type = GetType(xyz)
For Each p As System.Reflection.FieldInfo In t.GetFields()
sbOutput.AppendLine(p.Name)
Next
关于VB.NET 遍历结构的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8748587/
我是一名优秀的程序员,十分优秀!