2010-06-15 49 views
1

我在遍歷類屬性時遇到問題。每個類的屬性

我有我的第一類:

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,名稱,ItemCategory但我預計UIN,名稱和類別。

我對此有點不清楚,並且知道爲什麼會發生這種情況?我該怎麼做才能糾正它?

回答

1

我得到的UIN,名稱,ItemCategory

這是沒有意義的。您正在檢索屬性的基礎類型的名稱,您應該已經獲得類似System.String,System.Int32和ItemCategory的內容。

如果它的屬性你後,你可以只使用PropertyInfo.Name,你的情況:

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

呀propA.Name給我的結果,我想,但它並不會導致我的我期待的解決方案。其實我正在努力解決我的下一個問題,並認爲它給了問題。我的實際問題是: http://stackoverflow.com/questions/3042804/how-to-assign-class-property-as-display-data-member-in-datagridview – KoolKabin 2010-06-16 06:52:06