VB.NET 遍历结构的对象 [英] VB.NET Iterating through objects of a structure

查看:58
本文介绍了VB.NET 遍历结构的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构xyz",其中包含 3 个字符串对象."foo" "bar" 和 "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

伪:

For each x as object in xyz 
    if x.Name = "foo" then
        'bang
    end if
End each

这可能吗?

推荐答案

如果这只是一次性的事情,您可能会更轻松地使用字典,但如果您愿意,可以使用反射来做到这一点保持结构.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆