2016-06-08 61 views
1

object picture如何獲得動態對象的長度,我從Ajax函數中獲得對象,並且我試圖從動態對象中的數據構建一個列表,但它不起作用我使用了matchesData.lenght。動態對象的長度

public string updateQueue(dynamic matchesData) 
     { 
      MatchesQueues m = new MatchesQueues(); 
      List<MatchesQueues> MQ = new List<MatchesQueues>(); 
      for (int i = 0; i < matchesData.length; i++) 
      { 
       MatchesQueues matchqueue = new MatchesQueues(matchesData[i].FieldId, matchesData[i].MatchId, matchesData[i].NumInQueue); 
       MQ.Add(matchqueue); 
      } 

      int rowsAffected = m.updateQueue(MQ); 
      if (rowsAffected == 1) 
      { 
       return "Time Updated"; 
      } 
      else 
      { 
       throw (new Exception("Time didn't updated")); 
      } 
     } 

回答

0

由於matchesDatadynamic型的,沒有辦法,你知道它有一個length屬性,但如果你確信,它已嘗試訪問length屬性之前它轉到特定類型。例如,如果你知道它的string類型,然後將它轉換爲字符串類型,然後訪問像

var data = matchesData as string; 
if(data != null) 
{ 
for (int i = 0; i < data.length; i++) {...} 
} 
+0

屬性的問題是,它是一個複雜的對象,看到的畫面。這是我決定使用動態而不是列表的字符串列表的第一個原因。我添加了一個對象 –

+0

@MosheEzer的圖片,'matchesData'的實際類型是什麼?張貼類的定義爲更好地澄清?一張圖片不會有太大的幫助。 – Rahul