如何在VB中遍历类的属性? [英] How to iterate through a property of a class in VB?

查看:473
本文介绍了如何在VB中遍历类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个类对象A,并且它具有诸如a0,a1,a2之类的属性...如果此类具有100个这样的属性(最大为a99).我想显示所有这些属性,但是我不希望有100行如下调用的代码

If I have a class object A, and it has properties such as a0, a1, a2... If this class has 100 properties like this (up to a99). I would like to display each of these properties, but I do not want to have 100 lines of code of calling this as following

print A.a0
print A.a1
print A.a2
...
print A.a99

代码效率太低,所以我想知道是否有一种方法可以遍历这些属性.谢谢.

The code is too inefficient, so I am wondering if there is a way to loop through these properties. Thank you.

推荐答案

.NET提供了通过称为 reflection的过程在运行时检查对象的功能.原始文章的目的是以自动化的方式遍历对象的属性,而不是通过手动编码显示每个属性的显式语句来进行迭代,而反射是完成此操作的过程.

.NET provides the ability to examine an object at runtime through a process known as reflection. The purpose of the original post was to iterate through an object's properties in an automated fashion rather than by manually coding explicit statements that displayed each property, and reflection is a process to accomplish this very thing.

为此,在运行时遍历对象的属性,可以使用每种类型都可以使用的GetProperties()方法.在您的情况下,您要反射"的Type是A,因此GetProperties的特定于类型的版本将返回该对象的实例属性的列表.

For this particular purpose, looping through an object's properties at run-time, you use the GetProperties() method that is available for each Type. In your case, the Type you want to "reflect" is A, so the type-specific version of GetProperties returns a list of the instance properties for that object.

当您要求.NET返回对象的属性时,您还可以指定称为绑定标志的内容,该标志告诉.NET返回哪些属性-公共属性,私有属性,静态属性- BindingFlags枚举中大约二十种不同值的大量组合.为了便于说明,假设您的A0-A999属性声明为公共,那么BindingFlags.Public就足够了.要公开更多属性,只需将多个BindingFlag值与逻辑或"组合即可.

When you ask .NET to return the properties of an object, you can also specify what's called a binding flag that tells .NET which properties to return - public properties, private properties, static properties - a myriad of combinations from about twenty different values in the BindingFlags enumeration. For the purposes of this illustration, BindingFlags.Public will suffice, assuming your A0-A999 properties are declared to be public. To expose even more properties, simply combine multiple BindingFlag values with a logical "or".

因此,现在有了这些信息,我们要做的就是创建一个类,声明其属性,并告诉Reflection为我们枚举属性.假设您的A类已经存在,并且已经定义了属性名A0-A999,这是枚举以"A"开头的类的方法:

So, now armed with that information, all we need to do is create a class, declare its properties, and tell Reflection to enumerate the properties for us. Assuming your Class A exists with property names A0-A999 already defined, here's how you'd enumerate ones starting with "A":

// Assuming Class "A" exists, and we have an instance of "A" held in 
// a variable ActualA...
using System.Reflection

// The GetProperties method returns an array of PropertyInfo objects...
PropertyInfo[] properties = typeof(ActualA).GetProperties(BindingFlags.Public);
// Now, just iterate through them.
foreach(PropertyInfo property in properties)
{
    if (property.Name.StartsWith("A")){
    // use .Name, .GetValue methods/props to get interesting info from each property.
        Console.WriteLine("Property {0}={1}",property.Name,
                                             property.GetValue(ActualA,null));
    }
}

那里有.那是C#版本而不是VB,但是我认为一般概念应该很容易翻译.希望对您有所帮助!

There you have it. That's C# version rather than VB, but I think the general concepts should translate fairly readily. I hope that helps!

这篇关于如何在VB中遍历类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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