我正在尝试将数据写入文件,下面的代码包含一个writer
允许覆盖write()
基类方法的方法GH_Component
。代码编译没有错误,但数据没有按计划写入。我究竟做错了什么?
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);
}
}