0
我正在嘗試將數據寫入文件,下面的代碼包含一個writer
方法,該方法允許覆蓋基類GH_Component
的write()
方法。代碼編譯時沒有錯誤,但數據沒有按計劃寫入。我究竟做錯了什麼?通過覆蓋基類的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);
}
}
總是形容 「這是行不通的。」現在我不知道這是編譯時,運行時還是其他什麼。 –
@HenkHolterman謝謝,剛剛更正。 –
沒有看到基地級別和調用這將很難回答。無論如何,調試器是最好的選擇。 –