2011-12-06 92 views
0

我有一個類,出現這樣一些性能:映射屬性到數組索引

property1_1 {get;set;} 
property1_2 {get;set;} 
property1_3 {get;set;} 
... 
property9_1 {get;set;} 
property9_2 {get;set;} 
property9_3 {get;set;} 

這些特性需要被映射到索引中的陣列,例如:

array[0].property1 = property1_1 
array[0].property2 = property1_2 
array[0].property3 = property1_3 
... 
array[8].property1 = property9_1 
array[8].property2 = property9_2 
array[8].property3 = property9_3 

有大約有一百個這樣的屬性需要映射,我寧願不必通過索引單獨分配它們。我研究過使用反射和其他一些想法,但沒有人真的「感覺」更好。

任何想法?

謝謝。

+0

我相信你最好的選擇是反射,有沒有你沒有選擇反射的具體原因?我想你可以寫一個快速腳本來自動生成所有的作業,但我想這不是真正的重點...... –

+0

我不確定爲什麼「感覺」對於反射解決方案非常重要。這是最簡單,最直接的路線。 – asawyer

+0

你是什麼意思?因爲你可以在一個結構或類中組合三個屬性。如果你會然後生成一個數組,其中的元素是這種類型。還是我看到這個錯誤的方式? – RvdV79

回答

1

你可能會嘗試這樣的東西..?

public struct positionStruct { public string location; public int coordinateX; public int coordinateY; public int coordinateZ; }

public class Map 
{ 
    positionStruct [] positionArray = new positionStruct[100]; 


    public positionStruct this[int index] 
    { 
     get { return positionArray[index]; } 
     set { positionArray[index] = value; } 
    } 
} 

//That way you can access the array with Map[index]. Hope this helps