2011-12-06 91 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