VB.NET Iterating through objects of a structure

我只是一个虾纸丫 提交于 2019-12-07 17:06:01

问题


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

Is this possible?


回答1:


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.

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


来源:https://stackoverflow.com/questions/8748587/vb-net-iterating-through-objects-of-a-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!