2012-12-03 44 views
0

我基本上保存和載入值到一個數組喂成JSON庫,而我只是在尋找一個更優雅的方式來做到這一點:優雅的方式來保存/負載值到一個數組

類屬性陣列

return new object[] { path, pathDir, name }; 

數組類屬性

​​

理想情況下,簡單的解決方案不需要額外的運行時間開銷,如反射,我們感激。

我可以這樣做嗎?

c.{path, pathDir, name} = values[0...2] 

編輯:我專門詢問陣列。我知道序列化,JSON和Protobuf以及其他所有人都建議的內容。

+3

你應該看看[Json.NET(http://json.codeplex.com/) –

+1

你介意不使用反射當你的數據將通過網絡發送? –

+1

使用一個JSON庫,允許你直接餵你的類? –

回答

1

難道這不行嗎?

return new {path= "/Some/Path", pathDir= "SiteRoot", name="MyPath"} 

編輯:

//Mock function to simulate creating 5 objects with 'CreateArrayOb' function 
     public void CreatingObjects() 
     { 
      var lst = new List<object>(); 
      for (var x = 0; x < 5; x++) 
      { 
       lst.Add(CreateArrayOb(new string[] {"Path" + x, "Dir" + x, "Path" + x})); 
      } 
     } 
     public object CreateArrayOb(object[] vals) 
     { 
      if (vals != null && vals.Any()) 
      { 
       //Switch cases in the event that you would like to alter the object type returned 
       //based on the number of parameters sent 
       switch (vals.Count()) 
       { 
        case 0: 
         break; 
        case 1: 
         break; 
        case 2: 
         break; 
        case 3: 
         return new { path = vals.ElementAt(0), pathDir = vals.ElementAt(1), name = vals.ElementAt(2) }; 
       } 

      } 
      return null; 
     } 
+0

功能上是否相同? –

+0

@Geotarget不知道你在這裏問什麼。稍微詳細一點?另外,編輯我的帖子以包含其他代碼。 – TNCodeMonkey

相關問題