每个类的属性

发布于 2024-09-05 13:13:16 字数 753 浏览 3 评论 0原文

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

我有我的第一个类:

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、Name 和 Category。

我对此有点不清楚,不知道为什么会发生这种情况?我应该做什么来纠正它?

I am running from a problem while iterating over class properties.

I have my first class:

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

Now when i iterate over the class properties from following code

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

I get UIN, Name, ItemCategory but i expected UIN, Name and Category.

I am a bit unclear about this and dun know why this is happening? What should i do to correct it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜味超标? 2024-09-12 13:13:16

我得到 UIN、名称、ItemCategory

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

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

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

I get UIN, Name, ItemCategory

That doesn't make sense. You're retrieving the names of the underlying type of the property, you should've gotten something like System.String, System.Int32 and ItemCategory.

If it's the property names you're after, you can just use PropertyInfo.Name, in your case:

AllProps = new Item().getType().getProperties()
for each propA in AllProps
   debug.write propA.Name
next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文