在下面的代码中,我尝试将数组Filepaths
分配给变量m_settings
,但在LINQ方法之外无法识别Filepaths
。如何将FilePaths
的内容存储在我可以在SolveInstance
方法中使用的变量中?
public void ShowSettingsGui()
{
var dialog = new OpenFileDialog()
{
Multiselect = true,
Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
};
if (dialog.ShowDialog() != DialogResult.OK) return;
var paths = dialog.FileNames;
var FilePaths = paths.ToDictionary(filePath => filePath, File.ReadAllText);
}
private string[] m_settings = Filepaths;
protected override void SolveInstance(IGH_DataAccess DA)
{
if (m_settings == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
return;
}
DA.SetData(0, m_settings);
}
答案 0 :(得分:3)
我可能会读得太多,但我认为你不需要FilePaths,你可以直接设置m_settings ......
private Dictionary<string, string> m_settings;
public void ShowSettingsGui()
{
var dialog = new OpenFileDialog()
{
Multiselect = true,
Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
};
if (dialog.ShowDialog() != DialogResult.OK) return;
var paths = dialog.FileNames;
m_settings = paths.ToDictionary(filePath => filePath, File.ReadAllText);
}
protected override void SolveInstance(IGH_DataAccess DA)
{
if (m_settings == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
return;
}
DA.SetData(0, m_settings);
}
您还需要确保在ShowSettingsGui之后调用SolveInstance,否则m_settings将始终为null
答案 1 :(得分:1)
public void ShowSettingsGui()
{
var dialog = new OpenFileDialog()
{
Multiselect = true,
Filter = "Data Sources (*.ini)|*.ini*|All Files|*.*"
};
if (dialog.ShowDialog() != DialogResult.OK) return;
var paths = dialog.FileNames;
var FilePaths = paths.ToDictionary(filePath => filePath, File.ReadAllText);
// You need to add this
this.m_settings = FilePaths;
}
// You also need to change the type of m_settings from string[] to Dictionary<string, string>
private Dictionary<string, string> m_settings = Filepaths;
protected override void SolveInstance(IGH_DataAccess DA)
{
if (m_settings == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "You must declare some valid settings");
return;
}
DA.SetData(0, m_settings);
}