VB.NET迭代结构的对象 [英] VB.NET Iterating through objects of a structure
本文介绍了VB.NET迭代结构的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个结构xyz,里面有3个字符串对象。 foobar和abc我想迭代结构并比较对象的名称。
I have a structure "xyz" with 3 string objects in it. "foo" "bar" and "abc" I want to iterate through the structure and compare the names of the objects.
Structure xyz
dim foo as string
dim bar as string
dim abc as string
End Structure
Pseudo:
For each x as object in xyz
if x.Name = "foo" then
'bang
end if
End each
这可能吗?
推荐答案
如果这只是一次性事情,你可能会有更轻松的时间改为使用Dictionary,但如果你想保留结构,你可以用Reflection做到这一点。
If this is just a one time thing you're probably going to have an easier time using a Dictionary instead, but you could do this with Reflection if you prefer to keep the structure.
这个小代码片段将为你列出每个结构成员 StringBuilder
。
This little code snippet will list out each structure member for you in a 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迭代结构的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文