2012-09-15 44 views
0

我正在嘗試將數據寫入文件,下面的代碼包含一個writer方法,該方法允許覆蓋基類GH_Componentwrite()方法。代碼編譯時沒有錯誤,但數據沒有按計劃寫入。我究竟做錯了什麼?通過覆蓋基類的Write()和Read()方法將數據寫入文件?

  public class SettingsComponent : GH_Component 

     { 
      public SettingsComponent(): base("LoadSettings", "LoadSettings", "Loading ini", "Extra", "Silkworm") { } 

      public override void CreateAttributes() 
      { 
       m_attributes = new SettingsComponentAttributes(this); 
      } 

      string m_settings_temp; 
      string[] m_settings; 

      public void ShowSettingsGui() 
      { 
       var dialog = new OpenFileDialog { Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*" }; 
       if (dialog.ShowDialog() != DialogResult.OK) return; 

       m_settings_temp = File.ReadAllText(dialog.FileName); 
       m_settings = m_settings_temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); 
       ExpireSolution(true); 
      } 


      public override bool Read(GH_IO.Serialization.GH_IReader reader) 
      { 
       if (m_settings != null && m_settings.Length > 0) 
       { 
        Int32 a = m_settings.Length; 
        reader.TryGetInt32("StringCount", ref a); 
        for (int i = 0; i < m_settings.Length; i++) 
        { 
         reader.TryGetString("String", i, ref m_settings[i]); 
        } 
       } 
       return base.Read(reader); 
      } 


      public override bool Write(GH_IO.Serialization.GH_IWriter writer) 
      { 
       if (m_settings != null && m_settings.Length > 0) 
       { 
        writer.SetInt32("StringCount", m_settings.Length); 
        for (int i = 0; i < m_settings.Length; i++) 
        { 
         writer.SetString("String", i, m_settings[i]); 
        } 
       } 
       return base.Write(writer); 
      } 



      protected override void SolveInstance(IGH_DataAccess DA) 
      { 
       if (m_settings == null) 
       { 
        AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings"); 
        return; 
       } 
       else 
       { 
        DA.SetDataList(0, m_settings); 
       }  
      } 
+2

總是形容 「這是行不通的。」現在我不知道這是編譯時,運行時還是其他什麼。 –

+0

@HenkHolterman謝謝,剛剛更正。 –

+1

沒有看到基地級別和調用這將很難回答。無論如何,調試器是最好的選擇。 –

回答

1

我要重寫的write()和read()方法上GH_Component:

//deserializes the variable m_settings to make the data persistant 
     public override bool Write(GH_IWriter writer) 
     { 
      if (m_settings != null && m_settings.Length > 0) 
      { 
       writer.SetInt32("StringCount", m_settings.Length); 
       for (int i = 0; i < m_settings.Length; i++) 
       { 
        writer.SetString("String", i, m_settings[i]); 
       } 
      } 

      return base.Write(writer); 
     } 

     //deserializes the variable m_settings to make the data persistant 
     public override bool Read(GH_IReader reader) 
     { 
      m_settings = null; 

      int count = 0; 
      reader.TryGetInt32("StringCount", ref count); 
      if (count > 0) 
      { 
       System.Array.Resize(ref m_settings, count); 
       for (int i = 0; i < count; i++) 
       { 
        string line = null; 
        reader.TryGetString("String", i, ref line); 
        m_settings[i] = line; 
       } 
      } 

      return base.Read(reader); 
     }