2011-11-16 100 views
62

我目前設置我所有的類對象Record的值。C#迭代通過類屬性

這是我使用的填充此刻的紀錄,財產屬性的代碼。

// Loop through each field in the result set 
for (int i = 0; i <= resultItems.Length; i++) 
{ 

    Record newRecord = new Record() 
    { 
      itemtype = resultItems[i - (fieldCount - 0)], 
      itemdesc = resultItems[i - (fieldCount - 1)], 
      prodcode = resultItems[i - (fieldCount - 2)], 
      proddesc = resultItems[i - (fieldCount - 3)], 
      curstat = resultItems[i - (fieldCount -4)], 
      totfree = resultItems[i - (fieldCount -5)], 
      totphys = resultItems[i - (fieldCount -6)], 
      pcolgroup = resultItems[i - (fieldCount -7)], 
      scolgroup = resultItems[i - (fieldCount -8)], 
      totpo =  resultItems[i - (fieldCount - 9)], 
      totso =  resultItems[i - (fieldCount - 10)], 
      quality = resultItems[i - (fieldCount - 11)], 
      statusdesc = resultItems[i - (fieldCount - 12)], 
      groupcode = resultItems[i - (fieldCount - 13)], 
      qualitydes = resultItems[i - (fieldCount - 14)], 
      pcoldesc = resultItems[i - (fieldCount - 15)], 
      scoldesc = resultItems[i - (fieldCount - 16)], 
      pgroupdesc = resultItems[i - (fieldCount - 17)], 
    }; 
} 

我可以動態地迭代每個屬性而不用硬編碼所有的屬性名稱嗎?

事情是這樣:

// Create new Record instance 
Record newRecord = new Record(); 

for (int e = 0; e < propertyCount.Length - 1; e++) 
{ 
    newRecord[fieldname] = resultItems[i - (fieldCount - e)]; 
} 
+0

您是否嘗試過反射? http://stackoverflow.com/questions/997747/c-sharp-reflection-accessing-the-fields-of-a-struct – kol

+1

請看看這個鏈接http://stackoverflow.com/questions/721441/c -sharp-how-to-ite-through-classes-fields-and-set-properties –

+0

你能解釋一下你想在哪裏以及如何維護一個屬性和resultItems數組中索引之間的關係? –

回答

140

你可能使用反射來做到這一點。據我瞭解,你可以枚舉你的類的屬性並設置值。你必須嘗試一下,並確保你理解屬性的順序。有關此方法的更多信息,請參閱此MSDN Documentation

對於一個提示,你可能做這樣的事情:

Record record = new Record(); 

PropertyInfo[] properties = typeof(Record).GetProperties(); 
foreach (PropertyInfo property in properties) 
{ 
    property.SetValue(record, value); 
} 

哪裏value是你想要寫的(所以從resultItems陣列)的值。

+0

如果我想使用* property.Getvalue(someOtherClassObjectHavingExacltySameProperties)*,而不是什麼*值*? (我想這**物業.SetValue(objAppointment,property.GetValue(TEMP));?**,但它給了我,** { 「對象不匹配目標類型。」} **) –

+0

@MalikAsif我不知道我完全理解,但它聽起來像你正試圖加載一個不同類型的屬性。如果您通過示例在SO上發佈問題並將其鏈接到此處,我會爲您查找。 –

12
// the index of each item in fieldNames must correspond to 
// the correct index in resultItems 
var fieldnames = new []{"itemtype", "etc etc "}; 

for (int e = 0; e < fieldNames.Length - 1; e++) 
{ 
    newRecord 
     .GetType() 
     .GetProperty(fieldNames[e]) 
     .SetValue(newRecord, resultItems[e]); 
} 
+0

這看起來不錯,但我希望動態地做到這一點,而不是在那裏硬編碼字段名。這可能嗎?謝謝 – Luke

+0

此外,似乎'GetProperty'屬性不適用於我的類實例。 :/ – Luke

+0

nope,需要在resultItem索引和屬性名稱之間進行映射。 – jgauffin

1

是的,你可以把你的記錄類從屬性名稱映射到正確的屬性的索引。這將繼續從屬性名稱的所有綁定的財產在一個地方如:

public class Record 
{ 
    public string ItemType { get; set; } 

    public string this[string propertyName] 
    { 
     set 
     { 
      switch (propertyName) 
      { 
       case "itemType": 
        ItemType = value; 
        break; 
        // etc 
      } 
     } 
    } 
} 

或者,正如其他人所提到的,使用了反射。