每个类属性

时间:2010-06-15 12:35:52

标签: vb.net reflection class iteration

我在迭代类属性时遇到问题。

我有第一堂课:

class Item
  private _UIN as integer = 0
  private _Name as string = ""
  private _Category as ItemCategory = new ItemCategory()

  public Property UIN() as integer
  public property Name() as string
  public property Category() as ItemCategory 

end class

现在,当我从以下代码

迭代类属性时
Dim AllProps As System.Reflection.PropertyInfo()
Dim PropA As System.Reflection.PropertyInfo
dim ObjA as object

AllProps = new Item().getType().getProperties()
for each propA in AllProps
   ObjA = PropA.GetValue(myObj, New Object() {})
   debug.write ObjA.GetType().Name
next

我得到UIN,Name,ItemCategory,但我期望UIN,名称和类别。

我对此有点不清楚并且知道为什么会发生这种情况?我该怎么做才能纠正它?

1 个答案:

答案 0 :(得分:1)

  

我得到UIN,Name,ItemCategory

这没有意义。您正在检索属性的基础类型的名称,您应该得到类似System.String,System.Int32和ItemCategory的内容。

如果您正在使用属性名称,则可以在您的情况下使用PropertyInfo.Name

AllProps = new Item().getType().getProperties()
for each propA in AllProps
   debug.write propA.Name
next